mootools 元素追加_使用MooTools 1.2进行元素集合操作的快捷方式

mootools 元素追加

One mistake I've been making with MooTools is that I've been looping through element collections (retrieved by using $$ function) to add events and manipulate elements. When you've got an element collection, there's no need to loop through them. Why? Because you can simply add modification code to the element collection and MooTools does the looping internally. Let me illustrate my point below.

我使用MooTools犯的一个错误是,我一直在遍历元素集合(通过使用$$函数进行检索)来添加事件和操作元素。 当您拥有一个元素集合时,无需遍历它们。 为什么? 因为您可以简单地将修改代码添加到元素集合,然后MooTools会在内部进行循环。 让我在下面说明我的观点。

Let me use my mouseover accordion example code to illustrate my point.

让我使用鼠标悬停手风琴示例代码来说明我的观点。

MooTools循环示例(坏/长) (MooTools Looping Example (Bad/Long))


$$('.toggler').each(function(el) {
	el.addEvent('mouseenter',function(e) {
		el.fireEvent('click');
	});
});


Above, we're looping through each item to add the event. This works fine but it's extra code.

上方,我们遍历每个项目以添加事件。 这可以正常工作,但这是额外的代码。

MooTools收集示例(好/短) (MooTools Collection Example (Good/Short))


$$('.toggler').addEvent('mouseenter', function() { this.fireEvent('click'); });


In this snippet, we skip the looping process all together and simply apply the addEvent() code to the collection. We also use the this keyword as an element reference instead of the individual element object. Of course, MooTools loops through the element collection internally but the above example shows you how to achieve the same effect using much less code.

在此代码段中,我们一起跳过了循环过程,仅将addEvent()代码应用于集合。 我们还将this关键字用作元素引用,而不是单个元素对象。 当然,MooTools在内部循环遍历元素集合,但是上面的示例向您展示了如何使用更少的代码来实现相同的效果。

翻译自: https://davidwalsh.name/element-collection-manipulation-shortcut-using-mootools

mootools 元素追加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值