我正在尝试将两个HTML数据修饰为一个HTML标签。我正在尝试使用正则表达式。
HTML看起来像这样
var temp = document.getElementById('data').innerHTML;
console.log(temp);
<div id="data">
<strong>20 </strong>
<strong>0 </strong>
<strong>0 /-</strong>
</div>
我的扩展输出为<strong>2000/-</strong>
答案 0 :(得分:2)
// you should get the textContent instead of the innerHTML
var temp = document.getElementById('data').textContent;
// removes all spaces and
// surrounds the output with the <strong> element
var newhtml = `<strong>${ temp.replace(/\s/g,'') }</strong>`;
// replaces the innerHTML of the data with newhtml
document.getElementById('data').innerHTML = newhtml
<div id="data">
<strong>20 </strong>
<strong>0 </strong>
<strong>0 /-</strong>
</div>
答案 1 :(得分:0)
您不需要正则表达式,只需用
连接字符串即可。 str.concat(string2, string3, string4, etc)
并为所有强标签赋予一个单独的ID
但是,如果内容是动态的并且未进行硬编码,则可以遍历document.getElementById('data')的子节点,并获取每个子节点的textContent并以此方式进行连接。