mootools 选择器_在MooTools中创建自定义“:selected”伪选择器

mootools 选择器

A while back I read a very interesting article by MooTools core developer Jan Kassens about how to create a custom pseudo selector in MooTools. I was surprised at the ease in which one can add their own pseudo selector that I thought I'd add a pseudo selector that I found useful in jQuery: :selected. This pseudo selector would return option elements that are selected. Here's how I did it:

前一段时间,我读了MooTools核心开发人员Jan Kassens的一篇非常有趣的文章,内容涉及如何在MooTools中创建自定义伪选择器 。 我为自己可以添加自己的伪选择器的便捷性感到惊讶,我以为我会添加一个在jQuery ::selected中发现有用的伪选择器。 该伪选择器将返回选择的选项元素。 这是我的做法:

XHTML (The XHTML)


<select name="mysel" id="mysel" multiple="multiple">
	<option value="One">One</a>
	<option value="Two">Two</a>
	<option value="Three" selected="selected">Three</a>
	<option value="Four" selected="selected">Four</a>
</select>


A basic select element that allows for multiple selections and has two elements selected by default.

一个基本的select元素,它允许多个选择,并且默认情况下有两个元素被选中。

MooTools自定义伪选择器 (The MooTools Custom Pseudo Selector)


	Selectors.Pseudo.selected = function(){
		return (this.selected && 'option' == this.get('tag'));
	};


MooTools has an internal "Selectors.Pseudo" variable so all I needed to do was add a selected property. That property is a function that returns whether an individual returned elements should be added to the collection. In this case, if the element has the selected property as true, the element should be returned. Using my example above, the 2 matching elements will be returned.

MooTools有一个内部“ Selectors.Pseudo”变量,因此我要做的就是添加一个selected属性。 该属性是一个函数,该函数返回是否应将单个返回的元素添加到集合中。 在这种情况下,如果元素的selected属性为true,则应返回该元素。 使用上面的示例,将返回2个匹配元素。

What other pseudo selectors do you think would be useful in MooTools? Please share!

您认为在MooTools中还有哪些其他伪选择器有用? 请分享!

翻译自: https://davidwalsh.name/create-custom-pseudo-selector-mootools-selected

mootools 选择器

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值