自定义Jquery选择器

如果当前jQuery内置的选择器不够用,开发人员也可以扩展jQuery,实现用户自定义的选择器。

如下面创建一个具有绿色背景元素的选择器。

<script>
$(function(){
	// 通过扩展$.expr[":"]实现自定义选择器
	$.expr[":"].greenbg = function(element){
		return $(element).css("background-color") === "green";
	};
	var n = $(":greenbg").length;
	console.log("There are "+n+" green divs");
});
</script>

对于<div style="width:10;height:10;background-color:green"></div>即可匹配选中。

猜你喜欢

转载自blog.csdn.net/huangbaokang/article/details/83348167