在div中包裹流浪文本
如何选择“没有包含标签的任何东西”来在jQuery中添加包装器? 例如:
This should remain untouched I want to wrap this in div.red
结果就是这样
This should remain untouched I want to wrap this in div.red
您正在尝试选择文本节点。 试试这个( 小提琴 ):
$('.post').each(function() { var data = []; $(this).contents().each(function() { if ( this.nodeType === Node.TEXT_NODE ) { data.push(this); } }).end().append( $('').append(data) ); });
HTML :
This should remain untouched I want to wrap this in div.red
CSS :
.red{background:red}