[英]How can I select all elements which are into a wrapper?
My dom looks like this: 我的dom看起来像这样:
<div class="wrapper">
<span>something</span>
<span>something else</span>
<a href="#">link</a>
<div><b>whatever</b</div>
<span>another thing</span>
</div>
Now I want to get a sequence of <span>
s from the beginning of vale of div.wrapper
until a tag else than <span>
. 现在,我想从
div.wrapper
的开头开始直到<span>
以外的其他标签,获取一个<span>
的序列。 So here is the result: 因此,结果如下:
<span>something</span>
<span>something else</span>
Is there any idea how can I do that? 有什么想法我该怎么做?
Here is what I've tried: 这是我尝试过的:
$(".wrapper").find("span").html();
But it doesn't support that "sequence" part. 但是它不支持该“序列”部分。
As someone mentioned in the comment, you can use the nextUntil
with the combination of addBack
to add the first matching node: 正如评论中提到的那样,您可以将
nextUntil
与addBack
结合addBack
来添加第一个匹配的节点:
$('.wrapper > span:first').nextUntil(':not(span)').addBack().each(function() { console.log( $( this ).html() ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <span>something</span> <span>something else</span> <span>and more</span> <span>and once again</span> <span>last one</span> <a href="#">link</a> <div><b>whatever</b></div> <span>another thing</span> </div>
One possible solution is to narrow your selector, combined it with each()
, eg like this: 一种可能的解决方案是缩小选择器,将其与
each()
结合起来,例如:
Stack snippet 堆栈片段
$(".wrapper span:first-child, .wrapper span + span").each(function() { // get inner HTML //console.log( $( this ).html() ); // get outer HTML console.log( $( this )[0].outerHTML ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <span>something</span> <span>something else</span> <a href="#">link</a> <div><b>whatever</b></div> <span>another thing</span> </div>
In case where there can be any combination of element types, you need to make sure their selected index match their markup index. 如果元素类型可以任意组合,则需要确保其选定索引与标记索引匹配。
In the below sample that would mean that the 3rd span
in the selection will have index 2 but in markup it is the 6th element with an index of 5. 在下面的示例中,这意味着选择中的第三个
span
将具有索引2,但是在标记中,它是索引为5的第六个元素。
Stack snippet 堆栈片段
$(".wrapper span:first-child, .wrapper span + span").each(function(selected_index) { // if not same index, return if (selected_index != $( this ).index()) return; // get outer HTML console.log( $( this )[0].outerHTML ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <span>something</span> <span>something else</span> <a href="#">link</a> <div><b>whatever</b></div> <span>another thing</span> <span>something 2</span> <span>something else 2</span> </div>
<div class="wrapper">
<span>something</span>
<span>something else</span>
<a href="#">link</a>
<div><b>whatever</b></div>
<span>another thing</span>
</div>
// start
var spanning = true;
// loop
$('.wrapper').children().each(function() {
// if still good and this is a span
if (spanning && $(this).is('span')) {
console.log($(this).text());
// still good but 1st non-span encountered
} else if (spanning && $(this).not('span')) {
// "break"
spanning = false;
}
});
https://jsfiddle.net/5uogfd5j/5/ https://jsfiddle.net/5uogfd5j/5/
You can do something like this: 您可以执行以下操作:
var keepGoing = true; $(".wrapper").children().filter(function() { if (!$(this).is("span")) { keepGoing = false; } return keepGoing; }).each(function(i) { console.log("filtered element: " + i + ", content: " + $(this).text()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <span>something</span> <span>something else</span> <a href="#">link</a> <div><b>whatever</b></div> <span>another thing</span> </div>
Here I'm defining a switch ( keepGoing
) outside the filter
callback function and I'm setting it to false
once I encounter a non- span
element. 在这里,我在
filter
回调函数之外定义了一个开关( keepGoing
),一旦遇到非span
元素, keepGoing
其设置为false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.