jQuery 最接近的类选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16051196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery closest class selector
提问by olo
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2">Click me</div>
<div class="Level3">Information</div>
</div>
jQuery
jQuery
$('.Level2').click(function(){
$('.Level2').closest('.Level3').fadeToggle();
});
I wanted to select the closest level3 to fadeIn and fadeOut, but doesn't work. Is my syntax wrong? online Sample :http://jsfiddle.net/meEUZ/
我想选择最接近淡入和淡出的 level3,但不起作用。我的语法错了吗?在线示例:http: //jsfiddle.net/meEUZ/
回答by Rishabh
Try .next()
instead of .closest()
that traverses through the ancestors of the DOM element.
尝试.next()
而不是.closest()
遍历 DOM 元素的祖先。
Also you should use $(this)
rather than $('.Level2')
else it'll select ALL the .Level2
rather than the clicked one.
此外,您应该使用$(this)
而不是$('.Level2')
else 它将选择所有.Level2
而不是点击的。
You can also go for something like this - $(this).closest('.wrap').find('.Level3').fadeToggle();
.
你也可以这样做 - $(this).closest('.wrap').find('.Level3').fadeToggle();
。
Cheers!
干杯!
回答by ryanbrill
jQuery's .closest()method doesn't select sibling selectors, but parents. Looks like you're looking for the .siblings()method.
jQuery 的.closest()方法不选择兄弟选择器,而是选择父选择器。看起来您正在寻找.siblings()方法。
$('.Level2').click(function(){
$(this).siblings('.Level3').fadeToggle();
});
回答by Rooster
closest travels up the dom tree. it won't find something thats a sibling. you can use a find on a parent to achieve this
最近沿 dom 树向上移动。它不会找到兄弟姐妹的东西。您可以使用对父项的查找来实现此目的
$('.Level2').click(function(){
$(this).parent().find('.Level3').fadeToggle();
});
回答by Mr Singh
Yes, There are many method avaiable in Jquery to find closest of the DOMelement
是的,在 Jquery 中有很多方法可以找到最接近的DOM元素
$('.Level1').click(function(){
$(this).next('.Level3').fadeToggle();
});
$('.Level2').click(function(){
$(this).closest('.wrap').find('.Level3').fadeToggle();
});
$('.Level4').click(function(){
$(this).parent().find('.Level3').fadeToggle();
});
$('.Level5').click(function(){
$(this).siblings('.Level3').fadeToggle();
});
.level{background:Red;width:200px;height:40px;}
.Level3{background:blue;width:300px;height:50px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="Level1 level">Click me()sing next)</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level2 level">Click me(Using closest)</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level4 level">Click me(Usingh Parent)</div>
<div class="Level3">Information</div>
</div>
<div class="wrap">
<div class="Level5 level">Click me(Using Sibiling)</div>
<div class="Level3">Information</div>
</div>
回答by AL MaMun
Get a clear idea from the following code:
从下面的代码中得到一个清晰的想法:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$(".add").on("click", function () {
var v = $(this).closest(".division").find("input[name='roll']").val();
alert(v);
});
});
</script>
<?php
for ($i = 1; $i <= 5; $i++) {
echo'<div class = "division">'
. '<form method="POST" action="">'
. '<p><input type="number" name="roll" placeholder="Enter Roll"></p>'
. '<p><input type="button" class="add" name = "submit" value = "Click"></p>'
. '</form></div>';
}
?>
回答by Rutwick Gangurde
Yes! closest
starts the DOM search from the selector you pass to it, and goes upwards the DOM hierarchy, searching through the parents/ancestors. Use siblings
or next
instead.
Like this:
是的!closest
从您传递给它的选择器开始 DOM 搜索,并向上 DOM 层次结构,搜索父/祖先。使用siblings
或next
代替。像这样:
$('.Level2').click(function(){
$(this).siblings('.Level3').fadeToggle();
});