是否有一个jQuery等价于MooTools的Element()构造函数
Is there a jQuery equivalent of MooTools' Element() constructor?
我将为我的网站启动一个javascript报告引擎,并且已经开始使用MooTools进行一些原型设计。 我真的很喜欢能够做这样的事情:
function showLeagues(leagues) {
var leagueList = $("leagues");
leagueList.empty();
for(var i = 0; i<leagues.length; ++i) {
var listItem = getLeagueListElement(leagues[i]);
leagueList.adopt(listItem);
}
}
function getLeagueListElement(league) {
var listItem = new Element('li');
var newElement = new Element('a', {
'html': league.name,
'href': '?league='+league.key,
'events': {
'click': function() { showLeague(league); return false; }
}
});
listItem.adopt(newElement);
return listItem;
}
据我所知,jQuery的"采用"类型方法只接受html字符串或DOM元素。 有没有相当于MooTools'Element的jQuery?
编辑:我在这里寻找的一件大事是我的点击事件对链接的编程附件。
从语法上讲,使用 jQuery 可能更好,但使用起来可能更有效
document.createElement('li')
并且至少不需要字符串比较测试和次要令牌解析。
如果你坚持生成大量的DOM节点,Flydom也可能会引起你的兴趣。(理论上应该更快,但还没有测试过)
注意:在内部,jQuery("
")看起来有效地做到了这一点(过于简化):jQuery(matcher) --> function(matcher)
{
return jQuery.fn.init(matcher) --> function(matcher)
{
return this.setArray(
jQuery.makeArray(
jQuery.clean(matcher) --> function(matcher)
{
div = document.createElement('div');
div.innerHTML = matcher;
return div.childNodes;
}
)
);
}
}
因此,人们会假设" document.createElement
"因此是一个"要求",如果您知道自己想要什么(即:不要用$( datahere )
生成一些第三方数据),那么恕我直言,document.createElement
同样合乎逻辑,并且速度提升以避免大量的正则表达式和缓慢的字符串操作。
相比之下:jQuery(document.createElement('div'))
看起来它有效地做到了这一点(过于简化):
jQuery(matcher) --> function(matcher)
{
return jQuery.fn.init(matcher) --> function(matcher)
{
this[0] = matcher;
this.length = 1;
return this;
}
}
jQuery中也是如此。基本上要创建一个新元素,您只需输入所需的 HTML。
function showLeagues(leagues) {
var $leagueList = $("#leagues");
$leagueList.empty();
$.each(leagues, function (index, league) {
$leagueList.append(getLeagueListElement(league));
});
}
function getLeagueListElement(league) {
return $('<li></li>')
.append($('<a></a>')
.html(league.name)
.attr('href', '?league=' + league.key)
.click(function() {
showLeague(league);
return false;
})
)
;
}
相关文章:
- ES6构造函数返回基类的实例
- 使用Google Visualization动态调用构造函数
- javascript中对象构造函数中的var属性与this.properties
- 理解typescript中的构造函数接口
- 为什么构造函数不是构造函数
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 拦截对构造函数的调用
- 使用闭包共享构造函数参数
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 如何使用此从对象访问构造函数
- Javascript:为什么是构造函数's __proto__属性Empty(){}
- 当一个重要的构造函数参数丢失时应该发生什么
- Chrome Javascript日期构造函数错误
- 如何使用构造函数's的输出,以便将值插入到对象中
- 为什么在调用父构造函数时在[]中发送数据
- 构造函数函数闭包变量
- ES6类是否与构造函数相同
- 构造函数中的事件处理程序与构造函数外的事件处理函数的行为不同
- 是否有一个jQuery等价于MooTools的Element()构造函数