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实现上传图片的裁剪功能
最近因项目需求,需要在上传图片的时候先对图片裁剪,然后在上传,所以就有了本文的出现. 开始正文之前,要提一下这个图片的裁剪:图片的裁剪,有前端裁剪,也可以后端裁剪 前端的裁剪我知道的可以分为这么两种: ...
随机推荐
- Trove自动钓鱼脚本(国际服
#WinActivateForce ; Script config. Do NOT change value here, might working inproperly! global Versio ...
- Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe.
Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe. htt ...
- http发送
package cn.com.yitong.wdph.util; import java.io.BufferedReader;import java.io.InputStream;import jav ...
- How Interfaces Work in Go
research!rsc: Go Data Structures: Interfaces https://research.swtch.com/interfaces How Interfaces Wo ...
- Spring听课笔记(tg)AOP
好文:https://blog.csdn.net/javazejian/article/details/56267036 通过一个实例来理解 1. 需求:实现算术计算器,可以加减乘除,同时记录日志 ...
- Docker+Prometheus+Alertmanager+Webhook钉钉告警
Docker+Prometheus+Alertmanager+Webhook钉钉告警 1.环境部署 1.1 二进制部署 1.2 docker部署 1.2.1 webhook 1.2.2 alertma ...
- c++面试笔试集锦
1. char *const p 是指针常量,通俗的解释:指针本身是一个常量 也就是不能改变该指针的指向性,可以改变指向的量的值 const char *p 是常量指针,解释:指向常量的指针,指针指向 ...
- centos编译安装vim7.4
./configure --with-features=huge --enable-fontset --enable-gui=gtk2 --enable-multibyte --enable-pyth ...
- Linux-yum安装和相关命令
Linux-yum安装和相关命令 一 yum yum命令是在Fedora和RedHat以及SUSE中基于rpm的软件包管理器,它可以使系统管理人员交互和自动化地更细与管理RPM软件包,能够从指定的服务 ...
- nginx教程<二>(高可用)
1.nginx集群 对于访问量较大的网站来说,随着流量的增加单台服务器已经无法处理所有的请求,这时候需要多台服务器对大量的请求进行分流处理,即负载均衡. 而如果实现负载均衡,必须在网站的入口部署服务器 ...