使用jQuery,如何在所有h1标签内部的文本周围包装一个div类。例如,将文档中的所有
。我遇到了瓶颈,请给予帮助,谢谢!
<h1>
标签内不能使用<div>
元素。建议使用<span>
元素代替。使用纯JavaScript:
var h = document.getElementsByTagName('h1')[0],
d = document.createElement('span');
while(h.firstChild) d.appendChild(h.firstChild);
h.appendChild(d);
d.className = "wrap";
<h1>
吗?// Get and wrap the content from the h1
var content = $('h1').html();
content = '<div class="wrap">' + content + '</div>';
// Reinsert the content into the h1
$('h1').html( content );
<span>
标签包裹,而不是使用<div>
标签。 - Roko C. Buljan