在同一(或非)级别上按类选择最近的元素

时间:2014-12-08 21:52:33

标签: javascript jquery html jquery-selectors

如何选择具有的特定class最近的元素可能与可点击元素不在同一级别?

例如,让我们说这是输出:



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change D</p>
            </div>
            <p class="change-on-click">D</p>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change E</p>
            </div>
        </div>
    </div>
    <p class="change-on-click">E</p>
</div>
&#13;
&#13;
&#13;

我希望当我点击p.click时,它会找到最近的p.change-on-click元素。


就像这样,但遗憾的是我无法更改 HTML

&#13;
&#13;
$('.click').on('click', function(){
    $(this).closest('.parent').find('.change-on-click').css('color', 'red');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="parent">
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div class="parent">
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <div>
            <div>
                <p class="click">Click on this will change D</p>
            </div>
            <p class="change-on-click">D</p>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <div>
            <div>
                <p class="click">Click on this will change E</p>
            </div>
        </div>
    </div>
    <p class="change-on-click">E</p>
</div>
&#13;
&#13;
&#13;

Here's a fiddle with the second situation

4 个答案:

答案 0 :(得分:3)

这将需要一种自定义方法,因为jQuery API不支持这种类型的搜索。基本上,只需遍历父节点,直到找到click-on-change元素。

$('.click').click(function(){
  var node = this;
  while(node != document.body){
   var target = $(node).find('.change-on-click')
   if( target.length == 0 ){
    node = node.parentNode;
   }else{
    target.css('color','red');
    return;
   }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

答案 1 :(得分:2)

你可以这样做:

$('p.click').each(function(i){
    $(this).on("click", function(){$('.change-on-click').eq(i).css('color', 'red');});
});

http://jsfiddle.net/4ofajqxa/1/

答案 2 :(得分:1)

这是你正在寻找的吗? (通过while循环找到目标元素

$('.click').on('click', function() {
    var coc = $(this);
    while( !coc.prev().is( '.change-on-click' ) ) {
        coc = coc.parent();
    }
    coc.prev().css('color','red');
});

&#13;
&#13;
$('.click').on('click', function() {
    var coc = $(this);
    while( !coc.prev().is( '.change-on-click' ) ) {
        coc = coc.parent();
    }
    coc.prev().css('color','red');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

使用jQuery插件

&#13;
&#13;
$('p.click').on('click', function(){
    $(this).changeOnClick();
});

$.fn.changeOnClick = function() {
  return this.each(function() {
    var near = $(this);
    while( !near.prev().is('.change-on-click') ) {
      near = near.parent();
    }
    near.prev().css('color','red');
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

<强>更新

以下版本适用于目标元素位于单击元素之前或之后的情况:

$('.click').on('click', function() {
    var coc = $(this);
    while( !coc.prev().is( '.change-on-click' ) && !coc.next().is( '.change-on-click' ) ) {
        coc = coc.parent();
    }
    coc.prev().add( coc.next() ).filter( '.change-on-click' ).css('color','red');
});

DEMO

答案 3 :(得分:0)

您可以使用jQuery执行此操作,而无需创建额外的功能和类似的东西......

&#13;
&#13;
$('.click').on('click', function(){
    $('.change-on-click', $(this).parents('div')).css('color', 'red');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change D</p>
            </div>
            <p class="change-on-click">D</p>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change E</p>
            </div>
        </div>
    </div>
    <p class="change-on-click">E</p>
</div>
&#13;
&#13;
&#13;