Javascript 使用jquery从HTML获取多维数组
我有重复的div在一页下面。我需要选择outerbox ID和文本元素Javascript 使用jquery从HTML获取多维数组,javascript,jquery,arrays,multidimensional-array,Javascript,Jquery,Arrays,Multidimensional Array,我有重复的div在一页下面。我需要选择outerbox ID和文本元素 <div class="outerbox" id="12345"> <div class="headerbox" id="title12345"> <div class="text"> </div> <h2>Foo Bar </h2>
<div class="outerbox" id="12345">
<div class="headerbox" id="title12345">
<div class="text"> </div>
<h2>Foo Bar
</h2>
</div>
等
谢谢我认为这应该行得通:
var array = [];
$('.outerbox').each(function ()
{
var id = this.id;
var text = $(this).find('.text').next().text();
array[text] = id;
});
但是,您可能需要清理文本
,以修剪空格和换行符。编辑、更新
试一试
var-boxids=$('.outerbox').map(函数(){
返回[$.trim($(“.text+*”,this)[0].innerText),this.id]];
}).get();
$(“正文”).text(boxID);
console.log(boxids)代码>
富吧
巴福
如果可能,可以发布所需数组的文本[[]]
?不确定“数组看起来像”Foo Bar | Bar Foo
12345 | 67890
<代码>[id,文本]
?谢谢,我知道二维数组的布局像电子表格一样[Foo-Bar[1234]Bar-Foo[4567]]将是理想的输出,因此我可以将它们称为数组[0][0],然后获取Foo-Bar,然后获取数组[0][1]以获取1234当我重复此框时,我缺少了什么?看不清“失踪”是什么意思console.log(boxids[0][0]、boxids[0][1]、boxids[1][0]、boxids[1][1])代码>?如果上面有两个“outerbox”代码实例,一个称为foo-bar,另一个称为bar-foo,我得到的ID不同[foo-bar-bar-foo[12345][foo-bar-foo[67890]]
Foo Bar | Bar Foo
12345 | 67890
var array = [];
$('.outerbox').each(function ()
{
var id = this.id;
var text = $(this).find('.text').next().text();
array[text] = id;
});
var boxids = $('.outerbox').map(function() {
return [[$.trim($(".text + *", this)[0].innerText), this.id]] ;
}).get();