简体   繁体   English

javascript到css选择器

[英]javascript to css selector

I would like to convert this javascript line of code to CSS, I've been searching the site and google but haven't got straight answer. 我想将这个javascript代码行转换为CSS,我一直在搜索网站和谷歌,但没有得到直接的答案。

var riskCell20 = document.getElementById("risk.probablity.literal.l100").childNodes[2];

to something like 喜欢的东西

#risk.probablity.literal.l100:nth-child(2);

could not find the right syntax 找不到合适的语法

As the id has dots, which is the character for target a class, you could use the attribute selector. 由于id有点,这是目标类的字符,因此可以使用属性选择器。

[attribute="value"]

With it you can target an element's attribute, such as title , href , etc., and in this case its id . 有了它,你可以定位元素的属性,例如titlehref等,在这种情况下是id

Stack snippet sample 堆栈代码段

 [id="risk.probablity.literal.l100"] :nth-child(2) { color: red; } 
 <div id="risk.probablity.literal.l100"> <div>1</div> <div>2</div> <div>3</div> </div> 


Note, the nth-child is not zero based, the childNodes is, so if you want the third child ( childNodes[2] ), your nth-child should be 3 ( nth-child(3) ) 注意, nth-child不是基于零的, childNodes是,所以如果你想要第三个孩子( childNodes[2] ),你的nth-child应该是3( nth-child(3)

Since the . 自从. character has a special meaning on a selector, you may have to escape it: 字符在选择器上有特殊含义,您可能必须将其转义:

#risk\.probablity\.literal\.l100:nth-child(2);

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

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