0

我有以下 DOM 结构:

<div id="cont">
<ul>
    <li>
        <span class="c1"><span class="c2">...</span><span class="c3">...</span></span>
    </li>
    <li>
        <span class="c1"><span class="c2">...</span><span class="c3">...</span></span>
    </li>
    <li>
        <span class="c1"><span class="c2">...</span><span class="c3">...</span></span>
    </li>
    ...
    <li>
        <span class="c1"><span class="c2">...</span><span class="c3">...</span></span>
    </li>
</ul>
</div>

使用以下 jQuery:

$("#cont ul li").each(function (e) {
    $(this).bind("mousemove",function(d) {
        var ele = $(this + " .c2");
        var ele2 = $(this " .c3");
        //do stuff
    });
});

我想做的是访问相对于当前 dom 位置的“c2”和“c3”元素。我希望循环选择相对于 DOM 顶部的以下内容:

ele = $("#cont ul li span .c2");
ele = $("#cont ul li span .c3");
//Do Stuff
//Move to the next set of Li's
ele = $("#cont ul li span .c2");
ele = $("#cont ul li span .c3");
//Do Stuff
//Move to the next set of Li's
ele = $("#cont ul li span .c2");
ele = $("#cont ul li span .c3");
//Do Stuff
//Move to the next set of Li's
..
4

3 回答 3

4

尝试

$("#cont ul li").each(function (e) {
  $(this).bind("mousemove",function(d) {
    var ele = $(this).find(".c2");
    var ele2 = $(this).find(".c3");
    //do stuff
 });
});
于 2012-10-03T13:59:02.287 回答
4

用于.find()选择当前匹配元素的后代。

$("#cont ul li").each(function (e) {
    $(this).bind("mousemove",function(d) {
        var ele = $(this).find(".c2");
        var ele2 = $(this).find(".c3");
        //do stuff
    });
});
于 2012-10-03T13:59:23.733 回答
2

如果 li 始终是直接父母,您可以使用:

var elem, elem2;
$('.c1').bind("mousemove",function(d) {
  elem=$(this).parent().find('.c2');
  elem2=$(this).parent().find('.c2');
  //Do stuff
})
于 2012-10-03T14:01:10.297 回答