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实现上传图片的裁剪功能
最近因项目需求,需要在上传图片的时候先对图片裁剪,然后在上传,所以就有了本文的出现. 开始正文之前,要提一下这个图片的裁剪:图片的裁剪,有前端裁剪,也可以后端裁剪 前端的裁剪我知道的可以分为这么两种: ...
随机推荐
- Soul API 网关源码解析 03
目标 使用 soul 代理 dubbo 服务 dubbo 服务如何注册到网关的? dubbo 插件是如何工作的? 理清 http --> 网关--> dubbo provider 整条链路 ...
- Vim中的swp文件,在vim非正常退出时,再次编辑会出问题
vim中的swp即swap文件,在编辑文件时产生,它是隐藏文件,如果原文件名是data,那么swp文件名就是.data.swp.如果文件正常退出,则此文件自动删除.以下两种情况不会删除swp文件: V ...
- PB 级大规模 Elasticsearch 集群运维与调优实践
PB 级大规模 Elasticsearch 集群运维与调优实践 https://mp.weixin.qq.com/s/PDyHT9IuRij20JBgbPTjFA | 导语 腾讯云 Elasticse ...
- centos7+python3+selenium+chrome
一.安装GUI图形化界面 (1)安装GUI图形化界面 yum groupinstall "GNOME Desktop" "Graphical Administration ...
- 在IDEA中用三个jar包链接MongoDB数据库——实现增删改查
安装Robo 3T连接MongoDB数据库教程:https://blog.csdn.net/baidu_39298625/article/details/98845789 使用Robo 3T操作Mon ...
- loj10153二叉苹果树
有一棵二叉苹果树,如果数字有分叉,一定是分两叉,即没有只有一个儿子的节点.这棵树共 N 个节点,标号 1 至 N,树根编号一定为 1. 我们用一根树枝两端连接的节点编号描述一根树枝的位置.一棵有四根树 ...
- 你可能不知道的 transition 技巧与细节
CSS 中,transition 属性用于指定为一个或多个 CSS 属性添加过渡效果. 最为常见的用法,也就是给元素添加一个 transition,让其某个属性从状态 A 变化到状态 B 时,不再是非 ...
- 常用shell命令及其用法
ls(lsit) 1.作用:列出文件列表 2.用法:ls [-a|-A--] [filename|directory] ls ls -a:查看隐藏文件 ls -l:查看文件详细信息 ...
- 牛客编程巅峰赛S2第3场 Tree VI (树,dfs)
题意:给你一个\(n\)个点的完全\(k\)叉树的先序遍历序列\(a\),还原这颗树并且求所有两个端点的异或和. 题解:用dfs在还原树的时候,把子节点和父亲节点的异或贡献给答案,对于每个节点,我们找 ...
- 使用docker时报错“net/http: TLS handshake timeout”
问题原因 :该命令默认从docker远端镜像仓库中拉取镜像,但由于远端仓库的服务器是在国外,我们国内有的用户很可能都访问不到 解决:使用国内镜像仓库 docker pull registry.dock ...