简体   繁体   English

mootools:从HTML创建新的DOM元素

[英]mootools: Creating a new DOM element from an HTML

How to create DOM elements from string (pass from ajax) in Mootools? 如何在Mootools中从字符串(从ajax传递)创建DOM元素?

In jQuery a simple solution is $( elements ) 在jQuery中,一个简单的解决方案是$( elements )

var elements = '<i>This is italic</i><b>this bold</b>...';

简单为: Elements.from('<i>This is italic</i><b>this bold</b>')

Without a string, you would use the Element class: 如果没有字符串,则可以使用Element类:

var el = new Element('div#id.class', {
    text: 'My text',
});

With a string, you can check how it's one in Request.HTML, see here . 使用字符串,您可以在Request.HTML中检查它的形式, 请参见此处

var temp = new Element('div').set('html', response.html);
response.tree = temp.childNodes;
response.elements = temp.getElements(options.filter || '*');

Basically Mootools elements & DOM elements are the same, this is another SO questions which creates DOM nodes from HTML: Creating a new DOM element from an HTML string using built-in DOM methods or prototype Mootools元素和DOM元素基本上是相同的,这是另一个从HTML创建DOM节点的SO问题: 使用内置DOM方法或原型从HTML字符串创建新的DOM元素

From old Mootools forums, I found an interesting idea too: add a new method Element.fromString() or String.toElement() which would contain this logic. 在旧的Mootools论坛上,我也发现了一个有趣的想法 :添加一个新方法Element.fromString()或String.toElement(),其中将包含此逻辑。

I'm using the latest Mootools 1.6.0 . 我正在使用最新的Mootools 1.6.0
It throws Elements.from is not a function. 它抛出Elements.from不是函数。

This one works for me: 这对我有用:

var html = '<img src='+item.src+'>';
var el = new Element('li').set('html', html);

The working code: http://jsfiddle.net/chetabahana/qbx9b5pm/ 工作代码: http : //jsfiddle.net/chetabahana/qbx9b5pm/

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

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