首页 > 代码库 > sizzle分析记录: 自定义伪类选择器
sizzle分析记录: 自定义伪类选择器
可见性
- :hidden
- :visible
隐藏对象没有宽高,前提是用display:none处理的
jQuery.expr.filters.hidden = function( elem ) { // Support: Opera <= 12.12 // Opera reports offsetWidths and offsetHeights less than zero on some elements return elem.offsetWidth <= 0 && elem.offsetHeight <= 0;};jQuery.expr.filters.visible = function( elem ) { return !jQuery.expr.filters.hidden( elem );};
内容
- :contains(text)
- :empty
- :has(selector)
- :parent
获取文本内容通过indexOf匹配
"contains": markFunction(function( text ) { return function( elem ) { return ( elem.textContent || elem.innerText || getText( elem ) ).indexOf( text ) > -1; };}),
取空
递归这个节点,排除nodeType大于6的节点
// Contents"empty": function( elem ) { // http://www.w3.org/TR/selectors/#empty-pseudo // :empty is negated by element (1) or content nodes (text: 3; cdata: 4; entity ref: 5), // but not by others (comment: 8; processing instruction: 7; etc.) // nodeType < 6 works because attributes (2) do not appear as children for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { if ( elem.nodeType < 6 ) { return false; } } return true;},
查看包含
递归sizzle通过传递上下文对象,实现包含查找
"has": markFunction(function( selector ) { return function( elem ) { return Sizzle( selector, elem ).length > 0; };}),
匹配含有子元素或者文本的元素
"parent": function( elem ) { return !Expr.pseudos["empty"]( elem );},
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。