Это не очень элегантно, но вы можете перебирать дочерние элементы тега <dl>
, создавать массив для каждого набора тегов <dt>
/ <dd>
и .push
этого массива в выходной массив:
//setup two arrays, one as a final output and one that will hold each sub-array
var output = [],
temp = [];
//iterate through each child element of the `<dl>` element
$('dl').children().each(function () {
//if this element is a `<dt>` tag
if (this.tagName == 'DT') {
//if the `temp` array is not empty
if (0 in temp) {
//`.push` the `temp` array onto the `output` array
output.push(temp);
}
//add the text of this `<dt>` tag to the `temp` array as the first key (erasing any data that was inside the `temp` array)
temp = [$(this).text()];
} else {
//if the tag found was anything other than a `<dt>` tag (I'm assuming it's a `<dd>` tag) then `.push` its text into the `temp` array
temp.push($(this).text());
}
});
//make sure to add the last iteration of the `temp` array to the `output` array
output.push(temp);
//for the structure supplied in the question, the output looks like this: [["A", "A1"], ["B", "B1", "B2"]]
Демонстрацию этого кода можно найти по адресу: http://jsfiddle.net/qkjKp/