将jquery选择器应用于与悬停对应的对象
由于某些原因,最后一行的添加不能按预期工作,文档中的所有将jquery选择器应用于与悬停对应的对象,jquery,jquery-selectors,Jquery,Jquery Selectors,由于某些原因,最后一行的添加不能按预期工作,文档中的所有:first child都已修改,我只希望修改悬停对象中的:first child。我想尽了一切办法,不知道问题出在哪里 jQuery.fn.img=function(background,param,repeat) { $(this).css('background-image','url(/static/img/'+background+'.png') .css('background-repeat',
:first child
都已修改,我只希望修改悬停对象中的:first child
。我想尽了一切办法,不知道问题出在哪里
jQuery.fn.img=function(background,param,repeat)
{ $(this).css('background-image','url(/static/img/'+background+'.png')
.css('background-repeat',repeat)
.css('background-position',param+' top');}
$('#sliding-door li').hover(function(e)
{$(this).img('b1_middle_focus','left','repeat-x');
$(this).add(':first-child span').img('b1_left_focus','left','no-repeat');},
html:
<ul>
<li><a href="/href1"><span><span>Something</span></span></a></li>
<li><a href="/href2"><span><span>Another thing</span></span></a></li>
</ul>
更新
我明白你现在想做什么了。如果仍然需要在所有li
s上应用悬停函数,同时只测试一个子例程的第一个子例程,则可以使用If语句:
$('#sliding-door li').hover(function(e) {
$(this).img('b1_middle_focus','left','repeat-x');
if ($(this).is(':first-child')) {
$(this).find('span').img('b1_left_focus','left','no-repeat');
}
}, ...);
旧答案,忽略 如果您在
$(this)
的第一个子项中查找span
,则您打算使用$(this)。find()
而不是$(this)。add()
:
.add()
将文档级别上的:first-child span
选择器匹配的所有元素添加到$(this)
对象,这与您期望的不完全一样。请参阅jQuery文档:.add()
?你是说.find()
?实际上这不好,我想让它测试悬停的对象是否是第一个子对象,而不是选择之后的第一个子对象,这与你要求的不同。我可以看看你的HTML吗?这里有一个例子,这两个是如何工作的,我希望第二个和第一个一样,我想我会分别测试悬停的第一个和其他东西
$(this).find(':first-child span').img('b1_left_focus','left','no-repeat');