将 CSS 样式/类应用于 div
我有一些 div 元素
结构是
<div id="comment">
<div id="m1">...</div>
<div id="m2">...</div>
</div>
我想将一些CSS或类应用到评论的偶数/奇数内部div(或m1/m2 div)
所以我编码了这个,但它不起作用:(
$("div>div:even").addClass("evn");
我缺少什么?
I have some div elements
The structure is
<div id="comment">
<div id="m1">...</div>
<div id="m2">...</div>
</div>
I want to apply some CSS or Class to the even/odd inner div of comments (or to the m1/m2 div)
So i coded this, but it did not worked :(
$("div>div:even").addClass("evn");
What i'm missing ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:even
和:odd
是 0 索引,可能不会产生您正在寻找的结果。第一个元素是数字 0,它是偶数,因此它被:even
选择,而不是第二个。对于 1 索引,您缺少
:nth-child()
伪类:确保你也正确拼写了类名,我不知道你的CSS是否定义了
.evn
类...:even
and:odd
are 0-indexed, and may not produce results you're looking for. The first element is number 0, and that's even, so it gets selected by:even
, rather than the second one.For 1-indexing, you're missing the
:nth-child()
pseudo-class:Make sure that you spelled the class name correctly too, I don't know if your CSS defines an
.evn
class...此链接可以帮助您解决问题
首先,在“index.html”文件中定义表格和div,如下所示,
现在,我们需要编写用于在交替行中显示不同颜色的脚本,
过滤器“even”和“odd”可用于jQuery 用于选择元素的奇数或偶数索引。如上所示,奇数和偶数“div”的背景颜色使用 jQuery 的“css”方法和“奇数”和“偶数”过滤器进行更改,同样适用对于偶数和奇数“tr”,这意味着表的行。
This link can help you to solve your proble
First , define the tables and div as shown below in the “index.html” file,
Now, we need to write the script for displaying the different color in the alternate row,
The filters “even” and “odd” can be used in jQuery for selecting odd or even index of the elements.As you can see above the background color of the odd and even “div” are changed using the “css” method and “odd” and “even” filters of jQuery and the same applies for the even and odd “tr” which means for the row of table.