很抱歉我要提出这个问题,但是我没有成功地使用Jquery中的unwrap或replaceWith方法。
我的问题很简单:我需要删除一些HTML代码中的节点(jquery选择器),同时保留这些节点的子元素。
这里有一个Jsfiddle演示了我难看的代码所达到的目标https://jsfiddle.net/uddaeh1u/15/ (是的它可以工作...)
// var content : the html source code of the wysiwyg
var result = '';
$(content).contents().each(function(){
var addContent = '';
// textNode
if(this.nodeType == 3) {
// Text Node
result+= $(this).text();
} else if(this.nodeType == 1 && $(this).hasClass('atwho-inserted')) {
// if is an Object Node with the target class
// I only keep it's contents (means that ".atwho-inserted" is not kept)
result+= $(this).html();
} else {
// in any other case I keep it entirely
result+= this.outerHTML;
}
});
你能帮我找到一份更好的代码(包含unwrap方法)吗?
非常感谢你 :)
<p>
也被移除了。这是预期的吗? - n4m31ess_c0d3r