将HTML< dl>使用jQuery嵌套到嵌套的JavaScript数组中 [英] Turning HTML <dl> into a nested JavaScript array with jQuery
本文介绍了将HTML< dl>使用jQuery嵌套到嵌套的JavaScript数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML定义列表,我想使用jQuery将其转换为嵌套的JavaScript数组.最简单的方法是什么?感谢您的帮助.
I have an HTML definition list, and I want to use jQuery to turn that into a nested JavaScript array. What's the simplest way to do this? Thanks for your help.
我的输入看起来像这样:
My input looks like this:
<dl>
<dt>A</dt>
<dd>A1</dd>
<dt>B</dt>
<dd>B1</dd>
<dd>B2</dd>
</dl>
我希望我的输出看起来像这样:
I want my output to look like this:
[['A', 'A1'], ['B', 'B1', 'B2']]
推荐答案
var final_array = [];
$('dl dt').each(function(){
var dt_dds = []; /* array to hold the dt_dds */
dt_dds.push( $(this).text() ); /* push the dt */
dds = $(this).nextUntil('dt'); /* get all dd's until the next dt */
dds.each(function(){ dt_dds.push( $(this).text() )}); /** push all dd's into the array*/
final_array.push( dt_dds );
})
console.log( final_array );
这是小提琴.
这篇关于将HTML< dl>使用jQuery嵌套到嵌套的JavaScript数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文