jquery环绕文本

我有以下内容

hello this text has no span

在jquery中如何包装div或跨越“this text has span span”文本?

对于您当前的示例,这是我的一行解决方案:

 $("div span").detach().prependTo($("div").contents().wrap("").end()); 

演示: http //jsfiddle.net/awwTA/

它不是最好的解决方案,但可以帮助你……

 
hello this text has no span
var div = $('div#myDiv'); var span = $('span.myspan'); div.remove(span); div.html($('').text(div.text()));

我猜你知道我想做什么

您可以在父div中搜索任何文本(nodeType:3)元素,然后将每个文本包装在另一个div中。 见下面的例子:

 $('#myDiv').contents().filter(function(){ return this.nodeType === 3; }).wrapAll("
");
 .red { color: red; } 
  
Hello this text has no span