jquery 找到最近的同级同级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2310270/
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 find closest previous sibling with class
提问by daulex
here's the rough html I get to work with:
这是我开始使用的粗略 html:
<li class="par_cat"></li>
<li class="sub_cat"></li>
<li class="sub_cat"></li>
<li class="par_cat"></li> // this is the single element I need to select
<li class="sub_cat"></li>
<li class="sub_cat"></li>
<li class="sub_cat current_sub"></li> // this is where I need to start searching
<li class="par_cat"></li>
<li class="sub_cat"></li>
<li class="par_cat"></li>
I need to traverse from the .current_sub
, find the closest previous .par_cat
and do stuff to it.
我需要从 遍历.current_sub
,找到最接近的前一个.par_cat
并对其进行处理。
.find("li.par_cat")
returns the whole load of .par_cat
(I've got about 30 on the page). I need target the single one.
.find("li.par_cat")
返回全部负载.par_cat
(我在页面上有大约 30 个)。我需要针对单个。
Would really appreciate any tips :)
真的很感激任何提示:)
回答by karim79
Try:
尝试:
$('li.current_sub').prevAll("li.par_cat:first");
Tested it with your markup:
使用您的标记对其进行了测试:
$('li.current_sub').prevAll("li.par_cat:first").text("woohoo");
will fill up the closest previous li.par_cat
with "woohoo".
将li.par_cat
用“woohoo”填充最接近的前一个。
回答by Ed James
Try
尝试
$('li.current_sub').prev('.par_cat').[do stuff];
回答by eprothro
Using prevUntil() will allow us to get a distant sibling without having to get all. I had a particularly long set that was too CPU intensive using prevAll().
使用 prevUntil() 将允许我们获得一个遥远的兄弟姐妹,而不必获得所有。我有一个特别长的集合,它使用 prevAll() 占用太多 CPU。
var category = $('li.current_sub').prev('li.par_cat');
if (category.length == 0){
category = $('li.current_sub').prevUntil('li.par_cat').last().prev();
}
category.show();
This gets the first preceding sibling if it matches, otherwise it gets the sibling preceding the one that matches, so we just back up one more with prev() to get the desired element.
如果匹配,它会获取第一个前面的兄弟,否则它会获取匹配的兄弟之前的兄弟,因此我们只需使用 prev() 再备份一个以获得所需的元素。
回答by Tom McDonough
I think all the answers are lacking something. I prefer using something like this
我认为所有的答案都缺少一些东西。我更喜欢使用这样的东西
$('li.current_sub').prevUntil("li.par_cat").prev();
Saves you not adding :first inside the selector and is easier to read and understand. prevUntil() method has a better performance as well rather than using prevAll()
保存您不在选择器中添加 :first 并且更易于阅读和理解。prevUntil() 方法也比使用 prevAll() 方法有更好的性能
回答by AL MaMun
You can follow this 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>';
}
?>
You can get idea from this.
你可以从中得到想法。