这是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");
更多推荐
发布评论