how to create a style element in js (many ways)

create style in js

Constructed StyleSheets

CSSStyleSheet

adoptedStyleSheets

Shadow Roots (Shadow DOM)

Documents

demo

// Create our shared stylesheet:
const sheet = new CSSStyleSheet();
sheet.replaceSync('a { color: red; }'); // Apply the stylesheet to a document:
document.adoptedStyleSheets = [sheet]; // Apply the stylesheet to a Shadow Root:
const node = document.createElement('div');
const shadow = node.attachShadow({ mode: 'open' });
shadow.adoptedStyleSheets = [sheet];

https://developers.google.com/web/updates/2019/02/constructable-stylesheets

insertAdjacentHTML


document.head.insertAdjacentHTML("beforeend", `<style>body{background:red}</style>`)

styleSheet.cssText

   const styleNode = document.createElement('style');
styleNode.type = "text/css"; // browser detection (based on prototype.js)
if(!!(window.attachEvent && !window.opera)) {
styleNode.styleSheet.cssText = 'span { color: rgb(255, 0, 0); }';
} else {
var styleText = document.createTextNode('span { color: rgb(255, 0, 0); } ');
styleNode.appendChild(styleText);
} document.getElementsByTagName('head')[0].appendChild(styleNode);

innerHTML

  const style = document.createElement('style');

  style.innerHTML = `
#target {
color: blueviolet;
}
`; document.head.appendChild(style);

style.sheet.insertRule


const style = document.createElement('style'); style.sheet.insertRule('#target {color: darkseagreen}'); document.head.appendChild(style);

CSSStyleSheet

// Create our shared stylesheet:
const sheet = new CSSStyleSheet();
sheet.replaceSync('#target {color: darkseagreen}'); // Apply the stylesheet to a document:
document.adoptedStyleSheets = [sheet];

https://dev.to/karataev/set-css-styles-with-javascript-3nl5

style

const div = document.createElement('div');

div.style.color = "red";

document.head.appendChild(style);

setAttribute

https://www.w3schools.com/JSREF/met_element_setattribute.asp

const div = document.createElement('div');

div.setAttribute(`style`, `color: red;`)

document.head.appendChild(style);

refs

style element & web components

https://www.cnblogs.com/xgqfrms/p/13614365.html

https://stackoverflow.com/questions/524696/how-to-create-a-style-tag-with-javascript

https://www.w3schools.com/JSREF/prop_html_style.asp

https://www.w3.org/wiki/Dynamic_style_-_manipulating_CSS_with_JavaScript

https://www.geeksforgeeks.org/how-to-create-a-style-tag-using-javascript/



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


how to create a style element in js (many ways)的更多相关文章

  1. style element & web components

    style element & web components style.textContent style.setContent bug style.textContent const st ...

  2. element(vue.js)+django 整合

    近期开始接触Python,从web开发入门.尝试Django与vue整合,大概分3个阶段: 1.基于Django开发web后端 2.基于element开发好前端 3.前后端整合 参考:https:// ...

  3. React.js 样式组件:React Style

    点这里 React Style 是 React.js 可维护的样式组件.使用 React Native StyleSheet.create一样的样式. 完全使用 JavaScript 定义样式: ? ...

  4. 辨析element.offsetXxxx和element.style.xxxx

    DOM操作时,经常使用element.style属性,没错,element.style是属性,和几个offsetXxxx属性一样,概念是一样的. 但是style有几个属性,这几个属性和offsetXx ...

  5. Node.js NPM Tutorial: Create, Publish, Extend & Manage

    A module in Node.js is a logical encapsulation of code in a single unit. It's always a good programm ...

  6. AjaxUpLoad.js使用实现文件上传

    AjaxUpLoad.js的使用实现无刷新文件上传,如图. 图1 文件上传前 图2 文件上传后 1.创建页面并编写HTML [html] view plaincopy   上传文档: <div  ...

  7. Js收藏-转

    /** * <P> Title: JavaScript Util </P> * <P> Description: JavaScript 工具 </P> ...

  8. D3.js学习记录【转】【新】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 基于layui+cropper.js实现上传图片的裁剪功能

    最近因项目需求,需要在上传图片的时候先对图片裁剪,然后在上传,所以就有了本文的出现. 开始正文之前,要提一下这个图片的裁剪:图片的裁剪,有前端裁剪,也可以后端裁剪 前端的裁剪我知道的可以分为这么两种: ...

随机推荐

  1. LiteOS调测利器:backtrace函数原理知多少

    摘要:本文将会和读者分享LiteOS 5.0版本中Cortex-M架构的backtrace软件原理及实现,供大家参考和学习交流. 原理介绍 汇编指令的执行流程 图 1 汇编指令的执行顺序 上图1所示, ...

  2. 在Golang中如何正确地使用database/sql包访问数据库

    本文记录了我在实际工作中关于数据库操作上一些小经验,也是新手入门golang时我认为一定会碰到问题,没有什么高大上的东西,所以希望能抛砖引玉,也算是对这个问题的一次总结. 其实我也是一个新手,机缘巧合 ...

  3. AES加密模式

    https://baike.baidu.com/item/高级加密标准/468774 AES加密模式 对称/分组密码一般分为流加密(如OFB.CFB等)和块加密(如ECB.CBC等).对于流加密,需要 ...

  4. 小步前进之WCF简介

    WCF 前言 什么是WCF? 契约 合约 前言 在 .NET Framework2.0 以及前版本中,微软发展了 Web Service..NET Remoting 等通信支持. 如果要进行通信,对于 ...

  5. loj10009钓鱼___vector的调试

    题目描述 在一条水平路边,有 n 个钓鱼湖,从左到右编号为1,2,...,n .佳佳有 h 个小时的空余时间,他希望利用这个时间钓到更多的鱼.他从1  出发,向右走,有选择的在一些湖边停留一定的时间( ...

  6. LOJ10104Blockade

    POI 2008 Byteotia 城市有 n 个城镇,m 条双向道路.每条道路连接两个不同的城镇,没有重复的道路,所有城镇连通.输出 n 个数,代表如果把第i  个点去掉,将有多少对点不能互通. 输 ...

  7. 最简单直接地理解Java软件设计原则之接口隔离原则

    理论性知识 定义 接口隔离原则, Interface Segregation Principle,(ISP). 一个类对应一个类的依赖应该建立在最小的接口上: 建立单一接口,不要建立庞大臃肿的接口: ...

  8. 数组赋值到select

    function _oneClassData() { var options = []; $(oneClass).each(function (i, item) { var option = {}; ...

  9. 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入。

    package com.minxinloan.utils; import java.math.BigDecimal; public class Arith { // 源文件Arith.java: /* ...

  10. 802.1X

    1.简介 IEEE802 LAN/WAN委员会为解决无线局域网网络安全问题,提出了802.1X协议.后来,802.1X协议作为局域网端口的一个普通接入控制机制在以太网中被广泛应用,主要解决以太网内认证 ...