mootools取代了div的内容

Rau*_*net 3 javascript mootools

我有以下HTML代码

<div id="myDiv">
   <p>content</p>
</div>
Run Code Online (Sandbox Code Playgroud)

以及以下JS代码:

$('myDiv').set('html',htmlCode);
Run Code Online (Sandbox Code Playgroud)

问题是变量htmlCode是这样的:

<div id="myDiv"><p>another content</p></div>
Run Code Online (Sandbox Code Playgroud)

所以,当我运行JS代码时的结果是这样的:

<div id="myDiv">
   <div id="myDiv">
      <p>another content</p>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有没有办法使用"设置",以便它覆盖整个div?或另一种解决方案来获得类似的东西

<div id="myDiv">
   <p>another content</p>
</div>
Run Code Online (Sandbox Code Playgroud)

作为JS脚本的结果?我知道我可以改变变量htmlCode ...我只是想知道是否有另一个解决方案.

ste*_*ecb 6

Mootools提供简单的替换方法!

//new tmp element that contains the new div
var tmpDiv = new Element('div',{html:'<div id="myDiv"><p>another content</p></div>'});

//new div (first child of my tmp div) replaces the old 'myDiv' (that can be grabbed from the DOM by $)
tmpDiv.getFirst().replaces($('myDiv'));
Run Code Online (Sandbox Code Playgroud)


Osk*_*zyk 5

String.implement({
    replaces: function(toReplace) {
        Elements.from(this).inject(toReplace, 'after');
        toReplace.destroy();
    }
});

'<div id="a"><p>ipsum</p></div>'.replaces($('a'));
Run Code Online (Sandbox Code Playgroud)

这应该做.示例:http://jsfiddle.net/UvuwG/