简体   繁体   English

以类似数组的方式使用香草Javascript添加或注入多行CSS

[英]Adding or injecting more than one row of CSS with vanilla Javascript in an array-like way

I use the following code to make all text red in a webpage: 我使用以下代码将网页中的所有文本设为红色:

var myCss = document.createElement("style");
myCss.type = "text/css";
myCss.innerHTML = " * {color: red !important} ";
document.body.appendChild(myCss);

This code works but I need a way to insert a few rows of CSS code, instead just one. 这段代码有效,但是我需要一种方法来插入几行CSS代码,而不是只插入一行。

I tried this instead, but didn't work: 我尝试了一下,但是没有用:

var cssArray = ["
   * {color: blue !important}
   * {background: white !important}
   #myId {display: block}
   .myClass {display: inline-block}
"];

var myCss = document.createElement("style");
myCss.type = "text/css";
myCss.innerHTML = cssArray;
document.body.appendChild(myCss);

I went through many SE QA sessions and all I found deals with adding a single CSS row as I used above. 我经历了许多SE QA会话,所有发现的事情都与我上面使用的添加单个CSS行有关。 If there is no way to inject several CSS rows in one command, is there any "dirty" workaround you know? 如果无法在一个命令中注入多个CSS行,您是否知道任何“肮脏”的解决方法?

You can do multiple lines of css within a string if you want. 您可以根据需要在一个字符串中执行多行CSS。 You don't need an array to do this. 您不需要数组即可执行此操作。

For readability purposes, you can set your multiline style string using an ES6 template literal : 出于可读性考虑,您可以使用ES6模板文字设置多行样式字符串:

 // ES6 template literal `backtick` syntax var myCss = ` * {background: blue;} h2 {background: #333; color: red;}`, head = document.head || document.getElementsByTagName("head")[0], style = document.createElement("style"); style.type = "text/css"; style.styleSheet ? style.styleSheet.cssText = myCss : style.appendChild(document.createTextNode(myCss)); head.appendChild(style); 
 <h2>Styled with js</h2> 

See also this CodePen Demo . 另请参见此CodePen演示


Note: If you want to support IE or obscure browsers , you might not want to use es6 template literals. 注意:如果要支持IE或晦涩的浏览器 ,则可能不希望使用es6模板文字。

If that's the case, you can simply make a string of css: 如果是这样,您可以简单地创建一串CSS:

var myCss = "* {background: blue;} h2 {background: #333; color: red;}";

or you can concatenate the string on multiple lines. 或者您可以在多行上连接字符串。

var myCss = 
  "* {background: blue;} " +
  "h2 {background: #333; color: red;}";

You could use your original code as well and set your css string in the backtick syntax. 您也可以使用原始代码,并在反引号语法中设置CSS字符串。

The problem is that your code will insert the styles into the body. 问题在于您的代码会将样式插入到主体中。 This can produce a flash of unstyled content - see this answer for more information. 这可能会产生未样式化内容的提示 -有关更多信息,请参见此答案

Therefore I recommend inserting the styles into the head, which the code above does. 因此,我建议将样式插入到上面的代码中。


尝试myCss.setAtrribute("style","color: blue !important; background: white !important; display: block;");

As you are using array of string to set innerHTML, so you will require array index. 当您使用字符串数组设置innerHTML时,将需要数组索引。 Try using: myCss.innerHTML = cssArray[0]; 尝试使用: myCss.innerHTML = cssArray[0];

Just join the array strings before passing it to innerHTML . 只需join数组字符串,然后再将其传递给innerHTML

var cssArray = ["
   * {color: blue !important}
   * {background: white !important}
   #myId {display: block}
   .myClass {display: inline-block}
"];

var myCss = document.createElement("style");
myCss.type = "text/css";
myCss.innerHTML = cssArray.join('');
document.body.appendChild(myCss);

Please don't thumb up this answer. 请不要竖起这个答案。 Thumb up Dan Kreiger's answer instead. 改用Dan Kreiger的答案。

The following code is based on Dan Krieger's answer and is actually 99% like Dan's answer (I tried to suggest this as edit by my edit was rejected by the SE team so I publish in an answer). 以下代码基于Dan Krieger的答案,实际上与Dan的答案相似,为99%(我试图建议这样做,因为我的编辑被SE团队拒绝,所以我在答案中发布)。

It works fine also: 它也可以正常工作:


let myCss =`
   .site_mainTable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(4) {display: none}
   .site_mainTable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(5) {display: none}
   gallery-banner, .gallery-banner-gl {display: none}
`;

style = document.createElement("style");
style.type = "text/css";
style.styleSheet ? style.styleSheet.cssText = myCss : style.appendChild(document.createTextNode(myCss) );
   // IF... style tag has a styleSheet, put its cssText as myCSS. ELSE... createTextNode with the contents of myCSS for later use. 

head = document.head || document.getElementsByTagName("head")[0];
head.appendChild(style);

All credit to Dan Kreiger. 所有归功于Dan Kreiger。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM