在DOM元素中选择(Selecting inside a DOM element)

编程入门 行业动态 更新时间:2024-10-23 00:25:48
在DOM元素中选择(Selecting inside a DOM element)

这是html代码

<div class="extra-sub-block sub-block-experience"> <h6 style="display:inline;" id="exp-pos-0" class="extra-sub-block-head sub-block-head-experience">CEO</h6> </div> <div class="extra-sub-block sub-block-experience"> <h6 style="display:inline;" id="exp-pos-1" class="extra-sub-block-head sub-block-head-experience">COO</h6> </div>

有几种这样的类似结构。 现在我尝试从每个块中提取值。

var temp=document.getElementsByClassName('sub-block-experience'); var result=$(temp[0]+"#exp-pos-0");

这会引发错误。 我跟着在另一个DOM中选择元素

我也试过了

var temp=document.getElementsByClassName('sub-block-experience'); var result=temp[0].find('h6');

这不起作用。 我在这做错了什么 救命?

This is the html code

<div class="extra-sub-block sub-block-experience"> <h6 style="display:inline;" id="exp-pos-0" class="extra-sub-block-head sub-block-head-experience">CEO</h6> </div> <div class="extra-sub-block sub-block-experience"> <h6 style="display:inline;" id="exp-pos-1" class="extra-sub-block-head sub-block-head-experience">COO</h6> </div>

There are several such similar structures. Now I try to extract the values from each block.

var temp=document.getElementsByClassName('sub-block-experience'); var result=$(temp[0]+"#exp-pos-0");

This throws an error. I followed selecting element inside another DOM

I also tried

var temp=document.getElementsByClassName('sub-block-experience'); var result=temp[0].find('h6');

This doesn't work as well. What am I doing wrong here. Help?

最满意答案

要从所有块中提取值,可以使用.map()函数,如下所示:

var results = $('.extra-sub-block-head').map(function(){ return $(this).text(); })

演示

旁注:由于id在文档中是唯一的,因此可以使用id选择器直接访问元素,如var result= $("#exp-pos-0"); 而不是var result=$(temp[0]+"#exp-pos-0");

For extracting the values from all blocks, you can use .map() function as follows:

var results = $('.extra-sub-block-head').map(function(){ return $(this).text(); })

Demo

side note: Since id is unique in a document, you can directly access the element using id selector like var result= $("#exp-pos-0");instead of var result=$(temp[0]+"#exp-pos-0");

更多推荐

本文发布于:2023-08-01 15:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   DOM   element   Selecting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!