如何选择具有或的特定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;
我希望当我点击p.click
时,它会找到最近的p.change-on-click
元素。
就像这样,但遗憾的是我无法更改 HTML 。
$('.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;
答案 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');});
});
答案 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');
});
$('.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;
或强> (使用jQuery插件)
$('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;
<强>更新强>
以下版本适用于目标元素位于单击元素之前或之后的情况:
$('.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');
});
答案 3 :(得分:0)
您可以使用jQuery执行此操作,而无需创建额外的功能和类似的东西......
$('.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;