简体   繁体   English

第一个选择器不会首先过滤,但第一个选择器会过滤

[英]first selector doesn't filter first, but first-child does

I've used first many times but this time it doesn't work and no errors in console. 我已经使用过很多次了,但是这次没有用,控制台也没有错误。 I have this html: 我有这个HTML:

<ul>
<li class="iheader"><h1>TITLE</h1></li>
//some more list items
<li class="iheader"></li> 
</ul>

Now running this code : 现在运行以下代码:

$('.iheader:first').removeClass('iheader');

Removes both classes. 删除两个类。 I've also tried : 我也尝试过:

$('.iheader').filter(':first').removeClass('iheader');

And same result. 同样的结果。

However if i use first:child it does work when the html is as mentioned above. 但是,如果我使用first:child ,则当html如上所述时,它确实可以工作。

anyone may know why? 有人知道为什么吗? could this be related to the fact that i'm including jQuery mobile? 这可能与我包括jQuery mobile的事实有关吗?

尝试

$('.iheader:first-child').removeClass('iheader');

首先使用jQuery

$('.iheader').first().removeClass('iheader');

对于那些想知道为什么cmes解决方案有效的人,是因为jQuery可以选择使用CSS选择器,而:first-child是一个css选择器,它为元素的css指定了css,它是其父元素的第一个子元素。

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

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