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. fiddler安装以及使用说明

    一.fiddler fiddler是一个抓包工具,通过使用它抓包我们可以很清晰的看到抓的内容的协议,URL,参数等. 1.安装 在普通下载网站找到安装包,直接安装,点击下一步即可. 二.使用 1.se ...

  2. 有状态(Stateful)应用的容器化 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1020178

    有状态(Stateful)应用的容器化 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1020178

  3. css知识补充

    盒子模型的介绍: 在网页中基本上都会显示一些方方正正的盒子,这种盒子就被我们称为盒模型 盒子模型的五个属性: width,height,border(边框),padding(内边距),margin(外 ...

  4. Spring Cloud系列之Commons - 1. 背景与基础知识准备

    本文基于 Spring Cloud 2020.0 发布版的依赖 本系列会深入分析 Spring Cloud 的每一个组件,从Spring Cloud Commons这个 Spring Cloud 所有 ...

  5. XSS、CSRF、SSRF联系&区别,防御

    目录 区别和联系 防御 联系和区别 相同点: XSS,CSRF,SSRF三种常见的Web服务端漏洞均是由于,服务器端对用户提供的可控数据过于信任或者过滤不严导致的. 相同点: XSS,CSRF,SSR ...

  6. 查看Linux用的桌面是GNOME、KDE或者其他(转)

    http://superuser.com/questions/96151/how-do-i-check-whether-i-am-using-kde-or-gnome 1) pgrep -l &quo ...

  7. 1.VLAN

    1.定位:VLAN,即虚拟局域网(Virtual Local Area Network),一种将局域网设备从逻辑上划分成一个个网段,从而实现虚拟工作组的新兴数据交换技术.VLAN是将一个物理的LAN在 ...

  8. Maven多模块的2种依赖管理策略

    在Maven多模块的时候,管理依赖关系是非常重要的,各种依赖包冲突,查询问题起来非常复杂,于是就用到了<dependencyManagement>, 示例说明, 在父模块中: <de ...

  9. @AliasFor 注解

    Spring 框架提供了很丰富的注解可以让我们很方便的进行 Spring 配置,今天要讲的注解--@AliasFor之前你可能并没有关注过,因为平时开发时我们的确不太会用到. 我关注到这个注解是因为我 ...

  10. LA 3641 Leonardo的笔记本 & UVA 11077 排列统计

    LA 3641 Leonardo的笔记本 题目 给出26个大写字母的置换B,问是否存在要给置换A,使得 \(A^2 = B\) 分析 将A分解为几个循环,可以观察经过乘积运算得到\(A^2\)后,循环 ...