jQuery选择父元素同级 [英] jquery select parent elements sibling
本文介绍了jQuery选择父元素同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,让我看看是否可以在这里足够清楚地解释这一点. 我的HTML看起来像
Ok, lets see if I can explain this clearly enough here. My HTML looks like
<li>
<div class="info_left" rel="snack">Fries</div>
<div class="info_right">
<span class="info_remove">[Remove]</span>
<span class="info_favorite">[Favorite]</span>
</div>
</li>
<li>
<div class="info_left" rel="lunch">Burger</div>
<div class="info_right">
<span class="info_remove">[Remove]</span>
<span class="info_favorite">[Favorite]</span>
</div>
</li>
<li>
<div class="info_left" rel="4thmeal">Taco</div>
<div class="info_right">
<span class="info_remove">[Remove]</span>
<span class="info_favorite">[Favorite]</span>
</div>
</li>
如果您会注意到,则在div"info_right"内将看到删除"和收藏夹".当单击这2个选项之一时,我需要做的是获取同一li的"info_left" div的rel和text值.我已经尝试了几种方法,但是我认为我没有正确地确定父母,兄弟姐妹之间的组合,或者我不知道.无论哪种方式,希望有人可以把我扔掉.
If you will note you will see a "Remove" and "Favorite" inside a div "info_right". What I need to do when either one of those 2 options is clicked on is get the rel and text values of the same li's "info_left" div. I've tried a few ways but don't think I'm nailing the combo between parent(s), siblings correctly or I dunno. Either way hoping someone can toss me a bone.
推荐答案
应为:
$('.info_remove,.info_favorite').on('click', function() {
var $left = $(this).parent().siblings('.info_left');
var rel = $left.attr('rel');
var txt = $left.text();
...
});
这篇关于jQuery选择父元素同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文