Jquery firefox错误行为

Jquery firefox错误行为,jquery,css,Jquery,Css,Firefox中的代码从元素1开始,而Chrome中的代码从元素0开始。 我应该如何处理它?尝试使用eq()选择器在试图遍历的元素集中指定索引。您还应该尝试避免嵌套许多类选择器,因为这可能会导致性能不佳。试着用ID代替 另外,当您指定!重要标记,确保CSS样式和重要标记之间不留空格。下面的代码将向元素中的第一个post_homeclass元素添加一个padding left CSS属性,该元素的类同时为clear和middle $(' .middle.clear > .post_home

Firefox中的代码从元素1开始,而Chrome中的代码从元素0开始。 我应该如何处理它?

尝试使用eq()选择器在试图遍历的元素集中指定索引。您还应该尝试避免嵌套许多类选择器,因为这可能会导致性能不佳。试着用ID代替

另外,当您指定!重要标记,确保CSS样式和重要标记之间不留空格。下面的代码将向元素中的第一个post_homeclass元素添加一个padding left CSS属性,该元素的类同时为clearmiddle

 $(' .middle.clear > .post_home:first ').css('padding-left', '270px !important');

您可以通过以下方法在一小部分时间内实现类似效果:

$('.middle.clear .post_home').eq(0).css('padding-left', '270px!important');
!重要信息
在这里不需要,因为据我所知,内联样式始终优先

这工作得非常快,因为jQuery必须在内部运行几百个命令来创建jQuery对象,然后再运行一百个命令来设置CSS


但是请注意,IE7及以下版本不支持
查询选择器
,但提供了在IE7及以下版本中实现
查询选择器
的极好方法。

选择器,如
:first
:last
:n子元素(2)
在所有子元素匹配时工作

就你而言
$('.middle.clear>.post_home:first')
将假定您具有以下结构

document.querySelector(".middle.clear > .post_home").style.paddingLeft = "270px";

在这里,所有子元素都有类
post_home
,并且它们都由选择器匹配。 你有一些其他元素与它不匹配,所以你得到了这种异常行为。 尝试使用
$('.middle.clear>.post_home')[0].style.paddingLeft=“270px”
$('.middle.clear>.post_home').get(0.style.paddingLeft=“270px”


提示:如果您想使用这样的选择器,请使用
ul li
srstructure。

向我们展示HTML以及在FF和Chrome中选择的内容的示例(最好创建一个JSFIDLE)。我希望看到服务器有问题。我会和你一起尽快回来
<div class="middle clear">
 <div class="post_home">
 <!-- content -->
 </div>
 <!-- no other element of class other than post_home allowed here -->
 <div class="post_home">
 <!-- content -->
 </div>
 <!-- no other element of class other than post_home allowed here -->
 <div class="post_home">
 <!-- content -->
 </div>
 <!-- no other element of class other than post_home allowed here -->
</div>