Fork me on GitHub
随笔 - 265  文章 - 0  评论 - 1075  阅读 - 230万

sizzle分析记录: 自定义伪类选择器

可见性

隐藏对象没有宽高,前提是用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 );
};
复制代码

 

内容

获取文本内容通过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 );
},
posted on   【艾伦】  阅读(1598)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示