[英]How to apply css to id generated dynamically?
I am adding the index to star dynamically in html. 我在html中将索引动态添加为星号。
My html: 我的html:
<i class="fa fa-star fa-fw" id="star[111]" style="float:right;color:#D9CFBE"></i>
Script(in angularjs): 脚本(在angularjs中):
$scope.AddItem = function(obj) {
$("#star["+obj.Sno+"]")[0].style.color = "#FE5105";
}
But i am getting error: cannot read property style of undefined 但是我遇到了错误:无法读取未定义的属性样式
Kindly help me out plss 请帮助我
In a selector, [
and ]
delimit an attribute selector . 在选择器中,
[
和]
分隔属性选择器 。 If you want to match them as part of an id, you must escape them. 如果要将它们作为ID的一部分进行匹配,则必须对其进行转义。
Note that you must also escape \\
characters within JavaScript strings. 请注意,您还必须在JavaScript字符串中转义
\\
字符。
"#star\\[" + obj.Sno + "\\]"
It is generally better to avoid using CSS meta characters in ids and class names to begin with. 通常最好避免在ID和类名中使用CSS元字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.