replaceWith()
对我来说工作不正常。例如,给定这个HTML:replaceWith()不适用于克隆的分离节点
<div id="tree1">
<div>
<h3>Item 1</h3>
<h3>Item 2</h3>
<h3>Item 3</h3>
</div>
</div>
这个代码不<li>
小号取代<h3>
节点:
var items_Bad = $("#tree1 div h3").clone();
$("#tree1 div").remove();
$("#tree1").append ('<ul id="Items1"></ul>');
//--- This does not replace anything!
items_Bad.replaceWith (function() {
return ('<li>' + $(this).text() + '</li>');
});
$("#Items1").append (items_Bad);
但是这个代码! :
var items_Good = $("#tree1 div h3").clone();
$("#tree1 div").remove();
$("#tree1").append ('<ul id="Items1"></ul>');
$("#Items1").append (items_Good);
//--- But, this works!
$("#Items1 h3"). replaceWith (function() {
return ('<li>' + $(this).text() + '</li>');
});
在这里,只有在节点(重新)连接到DOM,但the documentation说replaceWith()
作品是:
对于jQuery 1.4,.replaceWith的()也可以在断开的DOM 节点上工作。
我错过了什么?
(请注意,我不控制原始页面的来源。而且,我有更多的操作去做,所以重新连接节点过早地操纵它们是不可取的。)
当我的代码基于jQuery 1.4.4时,replaceWith()用于正常工作。当我切换到jQuery 1.9.1(没有在代码中做任何其他更改)时,.replaceWith停止工作。我想我会留在jQuery 1.4.4中。参与jQuery项目的人似乎并不了解向后兼容的含义。 – 2013-02-16 18:50:08