简体   繁体   English

从具有特定类的div列表中获取div

[英]Get div from div list with specific class

I have list of div and want get div when contain specified class like state-expanded . 我有div list ,并希望在包含像state-expanded这样的指定类时得到div For example I have got two different divs : 例如,我有两个不同的divs

<div class="row-popular state-expanded no-sibling-group">
    <span class="cell cell-id-name" style="flex: 1 1 63px;">
       <span>TEST</span>
    </span>
</div>

<div class="row-popular state-collapsed no-sibling-group">
    <span class="cell cell-id-name" style="flex: 1 1 63px;">
       <span>OTHER</span>
    </span>
</div>

First div have state state-expanded and second state-collapsed . 第一个div有状态state-expanded和第二个state-collapsed I want get value of span where class in div is state-expanded . 我想要得到的值span ,其中类divstate-expanded

This list declaration: 这份list声明:

var allDivs = document.getElementsByTagName('div');

In example as I write above, It should return value TEST . 在我上面写的示例中,它应该返回值TEST Please only JavaScript - not jquery etc. 请只JavaScript - 而不是jquery等。

Any idea how get it? 知道怎么弄它?

You can use querySelectorAll to select all divs and then check each to see if it includes class DEMO 您可以使用querySelectorAll选择所有div,然后检查每个div以查看它是否包含类DEMO

var divs = document.querySelectorAll('div');
for (var i = 0; i < divs.length; i++) {
  if (divs[i].getAttribute('class').includes('state-expanded')) {
    console.log(divs[i].querySelector('span').innerHTML);
  }
}

Edit: also if you want to change text of that last span child you can do it like this Demo 编辑:如果你想更改最后一个跨栏孩子的文本,你可以像这个演示一样

You can use 您可以使用

document.getElementsByClassName('state-expanded') with javascript and if you are using jQuery. 使用javascript并使用jQuery的document.getElementsByClassName('state-expanded')

$('.state-expanded')

try this - var neededDiv = document.getElementsByClassName('state-expanded')[0] or you can also use querySelector like this: 试试这个 - var neededDiv = document.getElementsByClassName('state-expanded')[0]或者你也可以像这样使用querySelector

var neededDiv = document.querySelector('.state-expanded');

or 要么

var neededDiv = document.querySelector('.state-expanded span span');

if you need to get nested span with 'TEST' code in it.(According to your example) 如果您需要使用'TEST'代码获得嵌套跨度。(根据您的示例)

Here is another answer. 这是另一个答案。

var data  = document.getElementsByClassName('state-expanded')[0];
var data1 = data.getElementsByTagName('span')[1].innerHTML;

console.log(data1);

Output : TEST 输出: TEST

What it will done is, First in data variable it will get the 'state-expanded' div in it then in second line we are getting the second span of value(innerHTML) so it will return TEST. 它将要做的是,首先在数据变量中它将获得“状态扩展”div,然后在第二行中我们获得第二个值范围(innerHTML),因此它将返回TEST。

Here is a fiddle : https://jsfiddle.net/9p9ygd01 这是一个小提琴: https//jsfiddle.net/9p9ygd01

Try with this below code... 尝试使用以下代码...

 $(document).ready(function(){ var $element = $('.state-expanded span span').html(); alert($element); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="row-popular state-expanded no-sibling-group"> <span class="cell cell-id-name" style="flex: 1 1 63px;"> <span>TEST</span> </span> </div> <div class="row-popular state-collapsed no-sibling-group"> <span class="cell cell-id-name" style="flex: 1 1 63px;"> <span>OTHER</span> </span> </div> 

You may use querySelectorAll like: 您可以使用querySelectorAll

var txt = document.querySelectorAll('div.state-expanded span span')[0].textContent;

querySelectorAll will return the span element contained in a span that is contained in a div with the class state-expanded. querySelectorAll将返回包含在带有state-expanded类的div中的span中的span元素。

querySelectorAll returns a NodeList, but because in your case there is only one element you can take the first one (ie: [0]). querySelectorAll返回一个NodeList,但因为在你的情况下只有一个元素你可以获取第一个元素(即:[0])。

For the text contained inside you may use the textContent attribute. 对于包含在其中的文本,您可以使用textContent属性。

If instead you need to cycle on all the results obtained using querySelectorAll you need to convert NodeList to array and than use the forEach function like: 如果您需要循环使用querySelectorAll获得的所有结果,您需要将NodeList转换为数组,而不是使用forEach函数,如:

[].slice.call(document.querySelectorAll('div.state-expanded span span')).forEach(function(currValue, index) {
   alert(currValue.textContent);
});

 window.onload = function() { var txt = document.querySelectorAll('div.state-expanded span span')[0].textContent; document.body.innerHTML += '<br>Result: ' + txt; [].slice.call(document.querySelectorAll('div.state-expanded span span')).forEach(function(currValue, index) { document.body.innerHTML += '<br>Result in forEach loop: index=' + index + ' textContent=' + currValue.textContent; }); }; 
 <div class="row-popular state-expanded no-sibling-group"> <span class="cell cell-id-name" style="flex: 1 1 63px;"> <span>TEST</span> </span> </div> <div class="row-popular state-collapsed no-sibling-group"> <span class="cell cell-id-name" style="flex: 1 1 63px;"> <span>OTHER</span> </span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM