您可以执行以下操作
HTML
<description>
<div><b>Article_Title:</b> Title_Content</div>
<div><b>Article_Summary:</b> Summary_Content</div>
<div><b>Article_Date:</b> 05/08/2013</div>
</description>
Javascript
var descriptions = document.getElementsByTagName("description"),
results = [];
Array.prototype.forEach.call(descriptions, function (description) {
var result = {};
result.title = description.children[0].lastChild.textContent;
result.summary = description.children[1].lastChild.textContent;
result.date = description.children[2].lastChild.textContent;
results.push(result);
});
console.log(results);
输出
date: " 05/08/2013"
summary: " Summary_Content"
title: " Title_Content"
在jsfiddle 上
如果您不想要前导空格或尾随空格,请添加.trim()
到每个末尾textContent
仅当forEach
您定义了多个此类块时,才需要循环。