例如,我有字符串
'<p class="something">text goes here</p>'
我想把它解析成每个标签和内容的数组,即:
['<p class="something">', 'text goes here', '</p>']
答案 0 :(得分:0)
您尚未解释此用例,但此答案将提供相同的数据(在数组中组织有点不同),并且如果需要,可以使用数据重新创建元素。
var test = "<p class='something'>text goes here</p>";
// Set up an element that can contain the parsed string
var dummyElement = document.createElement("div");
// Parse/load the string into the element
dummyElement.innerHTML = test;
// Now we can extract metadata about the element and populate the array
var htmlData = [];
// Shortcut to element
var el = dummyElement.firstChild;
// Push node name into array:
htmlData.push(el.nodeName);
// Loop through attributes and put them into array:
for(var x = 0; x < el.attributes.length; x++){
htmlData.push(el.attributes[x].nodeName + "=" + el.attributes[x].nodeValue);
}
// Put element content (if it has any) into array
htmlData.push(el.textContent);
console.log(htmlData);
答案 1 :(得分:0)
这不是解决问题的更好方法,但可以帮助您。
LinkedHashMap