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. TCP为什么要三次握手与四次分手?

    TCP协议简介 TCP协议是五层协议中运输层的协议,下面依赖网络层.链路层.物理层,对于一个报文想发到另一台机器(假设是服务器)上对等层,每一个所依赖的层都会对报文进行包装,例如TCP协议就依赖网络层 ...

  2. 自导自演的面试现场,趣学MySQL的10种文件

    导读 Hi,大家好!我是白日梦!本文是MySQL专题的第 24 篇. 今天我要跟你分享的MySQL话题是:"自导自演的数据库面试现场--谈谈MySQL的10种文件" 换一种写作风格 ...

  3. 配置MySQL主从复制报错Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work

    配置MySQL主从复制报错 ``` Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave ha ...

  4. (转载)微软数据挖掘算法:Microsoft Naive Bayes 算法(3)

    介绍: Microsoft Naive Bayes 算法是一种基于贝叶斯定理的分类算法,可用于探索性和预测性建模. Naïve Bayes 名称中的 Naïve 一词派生自这样一个事实:该算法使用贝叶 ...

  5. WPF设计模式下选定数据源?F12直达ViewModel的方法,超好用

    您只需要在xaml上新增这一行代码,记得引用对应命名空间哦 d:DataContext="{d:DesignInstance viewModel:LoginViewModel, IsDesi ...

  6. 你可能不知道的 transition 技巧与细节

    CSS 中,transition 属性用于指定为一个或多个 CSS 属性添加过渡效果. 最为常见的用法,也就是给元素添加一个 transition,让其某个属性从状态 A 变化到状态 B 时,不再是非 ...

  7. flutter--Dart基础语法(二)流程控制、函数、异常

    一.前言 Flutter 是 Google 开源的 UI 工具包,帮助开发者通过一套代码库高效构建多平台精美应用,Flutter 开源.免费,拥有宽松的开源协议,支持移动.Web.桌面和嵌入式平台. ...

  8. HTML 5 学习第二课

    元素:<p>+++++++++</P> 全部内容 标签:<P></P> 属性:标签内部的内容 eg:<img src=" "& ...

  9. docker启动脚本

    #!/bin/bash # 定义环境变量 export LANG="en_US.UTF-8" #统一格式化打印输出信息 printMsg(){ echo "$(date ...

  10. Java泛型学习---第二篇

    泛型学习第一篇 1.泛型之擦拭法 泛型是一种类似"模板代码"的技术,不同语言的泛型实现方式不一定相同. Java语言的泛型实现方式是擦拭法(Type Erasure). 所谓擦拭法 ...