how to create a style element in js (many ways)
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)的更多相关文章
- style element & web components
style element & web components style.textContent style.setContent bug style.textContent const st ...
- element(vue.js)+django 整合
近期开始接触Python,从web开发入门.尝试Django与vue整合,大概分3个阶段: 1.基于Django开发web后端 2.基于element开发好前端 3.前后端整合 参考:https:// ...
- React.js 样式组件:React Style
点这里 React Style 是 React.js 可维护的样式组件.使用 React Native StyleSheet.create一样的样式. 完全使用 JavaScript 定义样式: ? ...
- 辨析element.offsetXxxx和element.style.xxxx
DOM操作时,经常使用element.style属性,没错,element.style是属性,和几个offsetXxxx属性一样,概念是一样的. 但是style有几个属性,这几个属性和offsetXx ...
- 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 ...
- AjaxUpLoad.js使用实现文件上传
AjaxUpLoad.js的使用实现无刷新文件上传,如图. 图1 文件上传前 图2 文件上传后 1.创建页面并编写HTML [html] view plaincopy 上传文档: <div ...
- Js收藏-转
/** * <P> Title: JavaScript Util </P> * <P> Description: JavaScript 工具 </P> ...
- D3.js学习记录【转】【新】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 基于layui+cropper.js实现上传图片的裁剪功能
最近因项目需求,需要在上传图片的时候先对图片裁剪,然后在上传,所以就有了本文的出现. 开始正文之前,要提一下这个图片的裁剪:图片的裁剪,有前端裁剪,也可以后端裁剪 前端的裁剪我知道的可以分为这么两种: ...
随机推荐
- QQ好友状态,QQ群友状态,究竟是推还是拉? 网页端收消息,究竟是推还是拉?
https://mp.weixin.qq.com/s/KB1zdKcsh4PXXuJh4xb_Zw 网页端收消息,究竟是推还是拉? 原创 58沈剑 架构师之路 2020-12-28 https:/ ...
- EMA algorithm: https://blog.csdn.net/m0_38106113/article/details/81542863
EMA algorithm: https://blog.csdn.net/m0_38106113/article/details/81542863
- Scala:case class
Scala:case class 1.Scala中class.object.case class.case object区别 1.1 class 和 object 关系 1.2 case class ...
- Jenkins ( jenkins+harbor)
Harbor 是咱们国产的docke仓库具体详细了解请参考以下连接 https://blog.csdn.net/csdn_duomaomao/article/details/78036331 http ...
- Go语言学习笔记(4)——并发编程
Golang在语言级别支持了协程,由runtime进行管理. 在Golang中并发执行某个函数非常简单: func Add(x, y int) { fmt.Println(x + y) } func ...
- 《C++ Primer Plus》啃书计 第1~4章
<C++ Primer Plus>啃书计 第1~4章 第一章 预备知识 1.1-1.3略过 1.4 程序创建的技巧 1. cfront,它将C++源代码翻译成C源代码,然后再使用标准C编译 ...
- 通过HBase Observer同步数据到ElasticSearch
Observer希望解决的问题 HBase是一个分布式的存储体系,数据按照RowKey分成不同的Region,再分配给RegionServer管理.但是RegionServer只承担了存储的功能,如果 ...
- 2019牛客暑期多校训练营(第四场)D-triples I
>传送门< 题意:求最少需要多少个3的倍数按位或后可以得到数字a 思路:利用3的倍数对应的二进制数的性质来先选出一个x,然后根据数字a再配一个y出来 首先,我们都知道十进制中,任意一个数只 ...
- hdu5491 The Next
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- Codeforces Round #496 (Div. 3) E1. Median on Segments (Permutations Edition) (中位数,思维)
题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...