[英]css that starts with selector ^= dont work after changing class with javascript
When loading page into browser everything looks fine and the background-image is correct. 将页面加载到浏览器时,一切看起来都很好,背景图像也是正确的。 But when I click on the icon and the javascript changes the class of the icon to .icon-myapp-cow the css from [class^="icon-myapp"] is ignored. 但是当我点击图标并且javascript将图标的类更改为.icon-myapp-cow时,将忽略来自[class ^ =“icon-myapp”]的css。 Only the css from icon-myapp-cow is applied. 只应用了icon-myapp-cow的css。 Tested in chrome and IE9 在chrome和IE9中测试过
My css 我的css
[class^="icon-myapp"] {
background-image: url("../img/myapp-icons.png");
}
.icon-myapp-horse {
background-position: -1px -1px;
height: 15px;
width: 15px;
}
.icon-myapp-cow {
background-position: -51px -1px;
height: 15px;
width: 15px;
}
My html 我的HTML
<i class="icon-myapp-horse icon-clickable"></i>
My javascript 我的javascript
$(".icon-clickable").click(function() {
$(this).removeClass("icon-myapp-horse");
$(this).addClass("icon-myapp-cow");
});
You need to change [class^="icon-myapp"]
to: 您需要将[class^="icon-myapp"]
更改为:
[class*="icon-myapp"]
See here for some more information, and here for a fiddle 请看这里获取更多信息,这里是一个小提琴
[attr^=value] Represents an element with an attribute name of attr and whose value is the (sic) prefixed by "value". [attr ^ = value]表示属性名称为attr的元素,其值为(sic), 前缀为“value”。
[attr*=value] Represents an element with an attribute name of attr and whose value contains at least one occurrence of string "value" as substring. [attr * = value]表示属性名称为attr的元素,其值至少包含一个字符串“value”作为子字符串。
Change the ^
to *
将^
更改为*
When you remove the class icon-myapp-horse
and then add icon-myapp-cow
this newly added class is appended to the class names so the new set of classes look like icon-clickable icon-myapp-cow
hence it does not start with icon-myapp
当您删除类icon-myapp-horse
然后添加icon-myapp-cow
这个新添加的类将附加到类名,因此新的类集看起来像icon-clickable icon-myapp-cow
因此它不会以icon-myapp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.