siblings() 没有按预期工作
我有一组图标,当您将鼠标悬停在一个图标上时,该组中的其他图像会淡出。我可以使用更长的选择器来做到这一点,但是我想尝试使用siblings()选择器,但我就是无法让它工作。我在这里缺少什么?谢谢
<div id="picks" class="section">
<div class="pick left">
<img src="images/p_mary.jpg" />
<div class="icons">
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
</div>
</div>
</div>
,然后是 javascript:
$("#picks").find("a > img").hover(function () {
$(this).siblings().stop().fadeTo(0, .3);
$(this).stop().fadeTo(0,1);
}, function () {
$(this).siblings().stop().fadeTo(500, 1);
});
I have a group of icons where when you hover over an icon, the other img's in the set fade out. I was able to do this using longer selectors, but I wanted to try using the siblings() selector, but I just can't get it to work. What am I missing here? Thanks
<div id="picks" class="section">
<div class="pick left">
<img src="images/p_mary.jpg" />
<div class="icons">
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
<a href="#" ><img src="images/i_imdb.png" /></a>
</div>
</div>
</div>
and then the javascript:
$("#picks").find("a > img").hover(function () {
$(this).siblings().stop().fadeTo(0, .3);
$(this).stop().fadeTo(0,1);
}, function () {
$(this).siblings().stop().fadeTo(500, 1);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样:
小提琴: http://jsfiddle.net/5jvmK/3/< /a>
性能提高
使用上述技术,它将为所有匹配的元素附加一个悬停事件。如果页面上有很多图标,性能可能会很低。相反,您可以使用
.delegate
来帮助提高性能:这会将事件附加到 JUST
.icons
上,并在匹配的选择器a 上触发该事件
。假设您有 20 个
,每个都有自己的图标,比如说 4 个图标。使用:
.hover
:它将向 DOM 附加 80 个事件 (20*4)。.delegate
:它只会附加 20 个。这性能提高了 400%。Fiddle: http://jsfiddle.net/5jvmK/5/
Like this:
Fiddle: http://jsfiddle.net/5jvmK/3/
Performance increase
Using the above technique it will attach an hover event for all the matched elements. If you have a lot of icons on the page the performance could be low. Instead, you could use
.delegate
to help increase the performance:This will attach an event onto JUST
.icons
and will fire on the event on the matched selectora
.Let's say you had 20
<div class="icons">
each with their own, let's say, 4 icons. With:.hover
: it'll attach 80 events to the DOM (20*4)..delegate
: it'll attach just 20. That's a 400% increase in performance.Fiddle: http://jsfiddle.net/5jvmK/5/
好吧,siblings() 函数的问题是它返回同一级别的兄弟姐妹。
例如,如果您执行以下操作:
这应该使链接消失,因为“a”标签位于同一级别,因此按照“siblings()”的工作方式是兄弟姐妹。
使用“a > img”选择所有 img 标签...但这些标签都没有同级...
看看这段代码:
这里的 img 标签有同级...我希望这对您来说足够清楚。
Well the problem with the siblings() function is that it returns siblings on the same level.
if you would do the following for example:
This should make the links fade because the "a" tags are on the same level and thus are siblings in the way "siblings()" is working.
Using "a > img" selects all img tags ... but none of those have siblings...
Take a look at this code:
Here the img tags have siblings ... I hope this is clear enough for you.
我认为选择器实际上是针对 img try:
对于所有可能的尝试:
I think that selector is actually targeting the img try:
For all possibly try: