jQuery 遍历 - 如何获取正确的元素
这是我的 html 代码。由于很多原因,这无法改变:
<ul class="menu">
<li>About Us
<div class="menuHover">
<ul class="left">
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: One</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Two</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Three</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Four</p>
</span>
</li>
</ul>
<img src="images/dottedline.png" width="3" height="120" />
<div class="right">
<img src="images/home.png" width="65" height="60" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing consectetur adipi Lorem ipsum dolor sit amet, consectetur</p>
</div>
</div>
</li>
<li>Contact
<div class="menuHover">
<ul class="left">
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: One</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Two</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Three</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Four</p>
</span>
</li>
</ul>
<img src="images/dottedline.png" width="3" height="120" />
<div class="right">
<img src="images/home.png" width="65" height="60" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing consectetur adipi Lorem ipsum dolor sit amet, consectetur</p>
</div>
</div>
</li>
</ul>
我追求的效果如下: 将鼠标悬停在 ul.menu .item
上时,将 .right
的内容替换为 .m-desc
。当然,由于有多个 .right
和 .m-dec
元素,我只是尝试引用正确的“最近”元素而不是全部。到目前为止我所拥有的是:
jQuery(function($) {
$('ul.menu .item').hover(function(){
$(this).closest('ul').next('.right').html($(this).children('.m-desc').html());
},function(){});
});
位 $(this).children('.m-desc').html()
引用正确。不过,我无法正确遍历这一点:$(this).closest('ul').next('.right').html()
。
$(this).closest('ul')
正确拉动,但是当我尝试添加 .next('.right')
时却没有。
我做错了什么?
Here is my html code. for a lot of reasons this cannot change:
<ul class="menu">
<li>About Us
<div class="menuHover">
<ul class="left">
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: One</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Two</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Three</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Four</p>
</span>
</li>
</ul>
<img src="images/dottedline.png" width="3" height="120" />
<div class="right">
<img src="images/home.png" width="65" height="60" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing consectetur adipi Lorem ipsum dolor sit amet, consectetur</p>
</div>
</div>
</li>
<li>Contact
<div class="menuHover">
<ul class="left">
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: One</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Two</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Three</p>
</span>
</li>
<li class="item"><a href="#" title="Link">Link</a>
<span class="m-desc">
<img src="../home.png" width="65" height="60" />
<p>This is a description: Four</p>
</span>
</li>
</ul>
<img src="images/dottedline.png" width="3" height="120" />
<div class="right">
<img src="images/home.png" width="65" height="60" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing consectetur adipi Lorem ipsum dolor sit amet, consectetur</p>
</div>
</div>
</li>
</ul>
The effect I'm after is as follows:
on hover over ul.menu .item
replace the contents of .right
with .m-desc
. Of course as there are multiple .right
and .m-dec
elements, I'm trying just to reference the correct 'nearest' ones rather than all. What I have so far is:
jQuery(function($) {
$('ul.menu .item').hover(function(){
$(this).closest('ul').next('.right').html($(this).children('.m-desc').html());
},function(){});
});
The bit $(this).children('.m-desc').html()
is referencing correctly. I can't get the traversing right for this bit though: $(this).closest('ul').next('.right').html()
.
$(this).closest('ul')
is pulling correctly, but when I try to add the .next('.right')
it's having none of it.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
.next('.something')
只会选择下一个同级(如果它与您指定的选择器匹配)。看起来.right
不是下一个,所以你什么也得不到。.nextAll()
选择以下所有同级,然后将其缩减为那些具有right
类的人。:first
然后仅选择其中的第一个(确保最多一个选定的元素)。Try this:
.next('.something')
will only select the next sibling if it matches the selector you've specified. It doesn't look like.right
is next, so you're getting nothing..nextAll()
selects all the following siblings, which you then reduce to those with classright
.:first
then selects just the first of those (ensuring at most one selected element).来自 关于 .next() 的 jQuery 文档:
尝试使用 .find() 代替。
From the jQuery docs on .next():
Try using .find() instead.