javascript或jquery中的第n个等效子级
我目前的问题是css中的第n个孩子在google chrome上的表现非常慢 因此,我想用javascript/jquery或其他语言等替代选项编写此代码,以实现完全相同的功能javascript或jquery中的第n个等效子级,javascript,jquery,css,css-selectors,Javascript,Jquery,Css,Css Selectors,我目前的问题是css中的第n个孩子在google chrome上的表现非常慢 因此,我想用javascript/jquery或其他语言等替代选项编写此代码,以实现完全相同的功能 div.testbox:nth-child(4n+6) { margin-left : 0px; margin-right : 1%; } div.testbox:nth-child(4n+3) { margin-left : 0px; margin-right : 0px; } 感谢您的帮助 这里是jQuery的等价
div.testbox:nth-child(4n+6) {
margin-left : 0px;
margin-right : 1%;
}
div.testbox:nth-child(4n+3) {
margin-left : 0px;
margin-right : 0px;
}
感谢您的帮助 这里是jQuery的等价物 例如: 选择作为其父元素第三个子元素的每个元素:
$("p:nth-child(3)")
语法:
:nth-child(n|even|odd|formula)
或使用eq()
选择第二个
元素:
$("p:eq(1)")
Syantax:
$(":eq(index)")
我会使用jQuery,它的语法与CSS几乎完全相同。这里是jQuery的等价物 例如: 选择作为其父元素第三个子元素的每个元素:
$("p:nth-child(3)")
语法:
:nth-child(n|even|odd|formula)
或使用eq()
选择第二个
元素:
$("p:eq(1)")
Syantax:
$(":eq(index)")
我会使用jQuery,它的语法几乎与CSS完全相同。它的语法基本相同。我做了一个测试来展示结果
$("div.testbox:nth-child(4n+6)").css('background-color', "#333");
$("div.testbox:nth-child(4n+3)").css('background-color', "#eee");
它的语法基本相同。我做了一个测试来展示结果
$("div.testbox:nth-child(4n+6)").css('background-color', "#333");
$("div.testbox:nth-child(4n+3)").css('background-color', "#eee");
以下是@Victory答案的纯JavaScript版本:
Array.prototype.slice.call(document.queryselectoral(“div.testbox:nth child(4n+6)”)).map(函数(i){
i、 style.backgroundColor=“#333”;
});
Array.prototype.slice.call(document.queryselectoral(“div.testbox:nth child(4n+3)”)).map(函数(i){
i、 style.backgroundColor=“#eee”;
});代码>
div.testbox:第n个子项(4n+6){
颜色:红色;
}
div.testbox:第n个子项(4n+3){
颜色:蓝色;
}
这里有一些文本
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
下面是一些文字
下面是@Victory答案的纯JavaScript版本:
Array.prototype.slice.call(document.queryselectoral(“div.testbox:nth child(4n+6)”)).map(函数(i){
i、 style.backgroundColor=“#333”;
});
Array.prototype.slice.call(document.queryselectoral(“div.testbox:nth child(4n+3)”)).map(函数(i){
i、 style.backgroundColor=“#eee”;
});代码>
div.testbox:第n个子项(4n+6){
颜色:红色;
}
div.testbox:第n个子项(4n+3){
颜色:蓝色;
}
这里有一些文本
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
这里有一些文字
下面是一些文本
不确定,但听起来像是您在寻找的内容?很可能是其他问题,如果jQuery的实现确实更快,我会感到惊讶。不确定,但听起来像是您在寻找的内容?很可能是其他问题,如果jQuery的实现确实更快,我会感到惊讶。