简体   繁体   English

jQuery $()。css(“content”)在IE9中返回字符串“normal”

[英]jQuery $().css(“content”) returning the string “normal” in IE9

I'm using the CSS content attribute to pass some values from my LESS stylesheet to JavaScript (to use some colors defined in LESS in Canvas elements). 我正在使用CSS content属性将一些值从我的LESS样式表传递给JavaScript(使用Canvas元素中的LESS中定义的一些颜色)。 To make my life easier I decided to place these values in a easy way to parse them in JavaScript. 为了让我的生活更轻松,我决定以简单的方式放置这些值,以便在JavaScript中解析它们。

LESS code: 少量代码:

div#colorChart-critical {
   content:'@{critical-highest},@{critical-veryhigh},@{critical-high},@{critical-low},@{critical-medium},@{critical-verylow}';
}

which when compiled brings the following CSS: 编译时带来以下CSS:

div#colorChart-critical6 {
    content: '#ff0000,#ff7200,#fffc00,#0000ff,#a200ff,#00ff00';
}

Then I try to read them using jQuery: 然后我尝试使用jQuery读取它们:

$("div#colorChart-critical").css("content").split(",");

The problem is that in IE9 calling $("div#colorChart-critical").css("content") is returning the string "normal" for some reason. 问题是在IE9中调用$("div#colorChart-critical").css("content")由于某种原因返回字符串"normal" Opera, Firefox, Safari and Chrome works fine. Opera,Firefox,Safari和Chrome工作正常。

Why does this happen in IE9? 为什么会在IE9中发生这种情况?

Any work-around this issue on IE9? 在IE9上解决这个问题? If not any other CSS atribute I can put random texts in? 如果不是任何其他CSS属性我可以随机文本?

I could use something like: 我可以使用类似的东西:

background: url(#ff0000,#ff7200,#fffc00,#0000ff,#a200ff,#00ff00);

But this would generate errors on the console. 但这会在控制台上产生错误。

It's because content as defined in CSS2.1 doesn't work on elements, only on the :before and :after pseudo-elements. 这是因为CSS2.1中定义的content不适用于元素,仅适用于:before:after伪元素。 IE9 is simply following the CSS2.1 spec here, which mandates that content on elements be computed to normal , always. IE9只是遵循这里的CSS2.1规范,它要求元素的content始终被计算为normal

I don't know why other browsers would return the value you have defined, especially considering that .css() makes use of getComputedStyle() on those browsers . 我不知道为什么其他浏览器会返回你定义的值,特别是考虑到.css() getComputedStyle()在这些浏览器上使用了getComputedStyle() If they're implementing CSS2.1 content , then they're violating CSS2.1 by not computing the value to normal . 如果他们正在实现CSS2.1 content ,那么他们通过不将值计算为normal来违反CSS2.1。 If they're preparing for a late CSS3 implementation, whatever that may be, then it would make sense that they implement it on actual elements somehow... shame on them either way. 如果他们正在准备一个晚期的CSS3实现,无论可能是什么,那么它们在某种程度上在实际元素上实现它是有道理的......无论如何都要羞辱它们。

Which brings me to another point: if you're not actually trying to use CSS to modify the content of an element, don't use content , even if the fact that it's not defined for use with elements is the reason you're making use of this technique in the first place. 这让我想到另一点:如果你实际上并没有尝试使用CSS来修改元素的content ,那么就不要使用content ,即使它没有定义用于元素的事实是你正在制作的原因首先使用这种技术。 You can try assigning those colors to certain classes, creating a hidden element and querying that element's color styles instead. 您可以尝试将这些颜色分配给某些类,创建隐藏元素并查询该元素的颜色样式。

BoltClock answer shows the cause of my problems. BoltClock答案显示了我的问题的原因。 I found a work-around by using the font-family instead of the content CSS property. 我通过使用font-family而不是内容CSS属性找到了解决方法。

My LESS code: 我的LESS代码:

div#colorChart-maincolors {
    font-family: '@{colorChart1},@{colorChart2},@{colorChart3},@{colorChart4},@{colorChart5},@{colorChart6}';
}

Which compiled into CSS gives: 编译成CSS的内容给出:

div#colorChart-maincolors {
  font-family: '#c0392b,#2980b9,#2ecc71,#f1c40f,#ecf0f1,#34495e';
}

The string can be acquired using: 可以使用以下方式获取字符串:

removeQuotes= function(string) {
   return string.replace(/^['"]+|\s+|\\|(;\s?})+|['"]$/g, '');
};
removeQuotes($("#colorChart-maincolors").css("font-family")); //add a .split(',') to get the colors as an array

The function removeQuotes is necessary because each browser adds a different kind of quotes into the return of getComputedStyle (and by extension the jQuery .css() method). 函数removeQuotes是必要的,因为每个浏览器在getComputedStyle的返回中添加不同类型的引号(并且通过扩展jQuery .css()方法)。 IE9 adds a double quote, Webkit adds a single quote. IE9添加了双引号,Webkit添加了单引号。

See this post on CSS tricks: http://css-tricks.com/making-sass-talk-to-javascript-with-json/ for more information. 有关CSS技巧,请参阅此文章: http//css-tricks.com/making-sass-talk-to-javascript-with-json/了解更多信息。

you can use replace(/["']/g, "") to remove extra quotation from string 你可以使用replace(/["']/g, "")从字符串中删除额外的引用

""string"" will be change to "string" ""string""将更改为"string"

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

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