简体   繁体   English

在样式表中插入多个 CSS 规则

[英]Insert Multiple CSS rules into a stylesheet

Okay, So I need to add rules to a style sheet, but it also needs to be cross browser, so I need multiple rules.好的,所以我需要向样式表添加规则,但它也需要跨浏览器,所以我需要多个规则。

Now I can get one working (Chrome) with a singular line.现在我可以用一条单行线得到一个工作(Chrome)。

However, I can't find any documentation or anything related to multiple lines.但是,我找不到任何文档或与多行相关的任何内容。

The idea is to add multiple css animations using browser prefixes as well, just to add to the fun.这个想法是使用浏览器前缀添加多个 css 动画,只是为了增加乐趣。

This is what I am trying to do (I'm debugging in Chrome):这就是我想要做的(我在 Chrome 中调试):

styleSheet.insertRule("@-webkit-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} }", styleSheet.cssRules.length);
styleSheet.insertRule("@-moz-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} }", styleSheet.cssRules.length);
styleSheet.insertRule("@-ms-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} }", styleSheet.cssRules.length);
styleSheet.insertRule("@keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} }", styleSheet.cssRules.length);

And then after this I need to add the browser prefix to each animation:然后在此之后我需要为每个动画添加浏览器前缀:

document.getElementById('starsColoured').style.webkitAnimationName = "userScore";
document.getElementById('starsColoured').style.mozanimationName = "userScore";
document.getElementById('starsColoured').style.MSAnimationName = "userScore";
document.getElementById('starsColoured').style.animationName = "userScore";

And it doesn't work, I'm not that surprised, I just don't know the actual way to add more than one.而且它不起作用,我并不感到惊讶,我只是不知道添加多个的实际方法。 Especially when I then try and add the prefixed animationname to the element.特别是当我尝试将前缀动画名称添加到元素时。

I've tried to put all the rules into one line, like so:我试图将所有规则放在一行中,如下所示:

styleSheet.insertRule("@-webkit-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} } @-moz-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} } @-ms-keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} } @keyframes userScore { 0% { width: 0px; } 100% {width: " + (monkeyScore * 20) + "px;} }", styleSheet.cssRules.length);

Again with no luck, this gives me the error of:再次没有运气,这给了我以下错误:

Uncaught SyntaxError: Failed to execute 'insertRule' on 'CSSStyleSheet': Failed to parse the rule '@-webkit-keyframes userScore { 0% { width: 0px; } 100% {width: 380px;} } @-moz-keyframes userScore { 0% { width: 0px; } 100% {width: 380px;} } @-ms-keyframes userScore { 0% { width: 0px; } 100% {width: 380px;} } @keyframes userScore { 0% { width: 0px; } 100% {width: 380px;} }'.

If I try them as a separate rule, I get the error of: Uncaught SyntaxError: Failed to execute 'insertRule' on 'CSSStyleSheet': Failed to parse the rule '@-moz-keyframes userScore { 0% { width: 0px;如果我将它们作为单独的规则尝试,我会收到以下错误: Uncaught SyntaxError: Failed to execute 'insertRule' on 'CSSStyleSheet': Failed to parse the rule '@-moz-keyframes userScore { 0% { width: 0px; } 100% {width: 380px;} }'. } 100% {宽度:380px;} }'。

I try it in FireFox, and I get the following error, relating to the first insertRule line (Chromes):我在 FireFox 中尝试,我收到以下错误,与第一个 insertRule 行(Chromes)有关:

SyntaxError: An invalid or illegal string was specified

So that's about it.就是这样。

Any help would be much appreciated!任何帮助将非常感激!

I am trying to avoid jQuery if I can help it.如果我能提供帮助,我会尽量避免使用 jQuery。

And I'm sure any documentation on multiple lines would also help me and others.而且我确信多行的任何文档也会对我和其他人有所帮助。

Thanks in advance提前致谢

You can insert a new stylesheet link with a data URI.您可以插入带有数据 URI 的新样式表链接。 Assuming your CSS rules are in a variable css , then:假设您的 CSS 规则位于变量css中,则:

head = document.getElementsByTagName('head')[0];
stylesheet = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'data:text/css,' + escape(css);  // IE needs this escaped
head.appendChild(link);

This works at least in the latest Chrome, Firefox and IE.这至少在最新的 Chrome、Firefox 和 IE 中有效。

Alternatively, you could add your rules one by one using insertRule and simply wrap each addition in a try block, so that the errors thrown by prefixed rules are ignored.或者,您可以使用insertRule添加规则,并将每个添加内容简单地包装在try块中,以便忽略前缀规则引发的错误。

How about just modifying a <style> tag?只修改<style>标签怎么样?

<style id="custom-styles"></style>

<script>
  var red = '#f00';
  document.getElementById('custom-styles').innerHTML = 'body { color: ' + red + '; }';
</script>

Here's a small demo: http://jsbin.com/mamekame/2/edit?html,js,output这是一个小演示:http: //jsbin.com/mamekame/2/edit ?html,js,output

Issue occurred due to space in the style element由于样式元素中的空间而出现问题

@keyframes userScore @keyframes 用户评分

Try like below @keyframes_userScore尝试如下@keyframes_userScore

The parsing is failing because you have '@' character in the style name.解析失败,因为样式名称中有“@”字符。 Try removing the '@' from or replae it with '_' and it will work.尝试从'@'中删除'@'或用'_'替换它,它会起作用。

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

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