简体   繁体   English

mootools取代了div的内容

[英]mootools replace the content of a div

i have the following HTML code 我有以下HTML代码

<div id="myDiv">
   <p>content</p>
</div>

and the following JS code: 以及以下JS代码:

$('myDiv').set('html',htmlCode);

the problem is that the variable htmlCode is something like: 问题是变量htmlCode是这样的:

<div id="myDiv"><p>another content</p></div>

so, the result when i run the JS code is something like: 所以,当我运行JS代码时的结果是这样的:

<div id="myDiv">
   <div id="myDiv">
      <p>another content</p>
   </div>
</div>

is there a way to use "set" so that it overrides the entire div? 有没有办法使用“设置”,以便它覆盖整个div? or another solution to get something like: 或另一种解决方案来获得类似的东西

<div id="myDiv">
   <p>another content</p>
</div>

as the result from the JS script? 作为JS脚本的结果? i know i could just change the variable htmlCode... i just was wondering if there's another solution to this. 我知道我可以改变变量htmlCode ...我只是想知道是否有另一个解决方案。

Mootools offers a simple replaces method! 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'));
String.implement({
    replaces: function(toReplace) {
        Elements.from(this).inject(toReplace, 'after');
        toReplace.destroy();
    }
});

'<div id="a"><p>ipsum</p></div>'.replaces($('a'));

This should do. 这应该做。 Example: http://jsfiddle.net/UvuwG/ 示例: http//jsfiddle.net/UvuwG/

$('myDiv').empty();
$('myDiv').adopt(Elements.from(htmlCode).getElement('p'));

你可以做

$("myDiv").getParent().set("html", htmlCode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM