简体   繁体   English

如何选择包装器中的所有元素?

[英]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: 正如评论中提到的那样,您可以将nextUntiladdBack结合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.

相关问题 如何使用jquery查找并选择具有以特定单词开头的数据属性的所有元素? - How can I find and select all elements which have data attribute start with specific word with using jquery? 如何选择两个元素之间的所有元素 - how can I select all elements between two elements 如何选择具有相同类名的所有元素? - How can I select all elements with the same class name? 我有一个带有选择的创建元素的表单。 我该怎么做才能创建一次元素? - I have a form with select which creates elements. How I can do that elements creates only once? 如何使用jQuery将多个元素包装到包装器中? - How can I wrap multiple elements in a wrapper with jQuery? 如何选择不在Jquery的隐藏父级中的所有表单元素? - How to select all form elements which are not in a hidden parent in Jquery? 如何选择所有以GB开头的元素? - How to select all the elements which class starts with GB? 如果将鼠标悬停在某个元素上,如何在jQuery中选择所有包含并包含它的同级元素? - If I hover over an element, how can I select all sibling elements before it and including it, in jQuery? 如何在“主要”元素中选择所有“子”元素 - How do I select all “sub” elements inside “main” elements 我如何编写一个函数,如果它的参数是节点集合,则为 javascript 中的所有节点元素执行? - How can i write a function, which if it's parameter is a node collection, executes for all node elements in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM