jquery选择器用法

原创
小哥 2年前 (2023-05-15) 阅读数 43 #大杂烩

jquery基本选择器(重要):

id:id选择器;

Element:元素选择器;

Class:类选择器;

jquery层次选择器:

ancestor descendant:显示选择ancestor 一切都在descendant元素;

parent>child:选择parent子元素的元素;

注意:ancestor descendant和parent>child区别:选择所有/选择你自己的孩子

select+:获得下面的兄弟元素(只删除一个);

select~:获得下面的兄弟元素(把所有后续元素);

:first/last:第一/最后一个元素;

实例


    
  • a
  • b
  • c
  • d
  • e
  • f

:even/odd:偶数/奇怪的元素;0开始)

:eq(index):指定索引位置元素;

:gt(index)/lt(index):大于/小于index所有元素的下标;

:header:找h标签元素(不需要找到一个父元素,您可以直接:header);

:not(“select”):删除选择select元素;

jquery内容选择器:

:contains(“text”):包含匹配text元素的内容;

例如:选择内容包括333的div ,背景变成红色:




    
111
222
333
444

:empty:匹配内容与空的内容;

:parent:选择元素作为父元素;

:has(selecter):选择包括selecter选择器;

实例:选择包括p的div背景颜色更改为红色:


    
111
222
333
444

1

jquery属性选择器:

        $("input").click(function(){
            $("img[src]").css({background:"red"});
            $("img[src=a.jpg]").css({background:"pink"});
        });
$("img[src^=a]").css({background:"pink"});

[attribute $= “value”]:找到属性value结束元素;.jpg 的g)

$("img[src $=g]").css({background:"pink"});
$("img[src *=a]").css({background:"pink"});

jquery子选择器(伪类选择器):

分为Type阵营,child阵营:

:first-child/last-child:找到第一个/最后子元素;

:first-of-type/last-of-type:找到第一个/最后子元素;(一模一样的功能);

    $("input").click(function(){
            $("li:first-child").css({background:"red"});
            $("li:last-of-type").css({background:"pink"});
        });

:nth-child(n) / :nth-of-type(n):找到第n子元素;

$("li:nth-child(2)").css({background:"red"});

:nth-last-child(n) / :nth-last-of-type(n):发现最后最后n子元素;

            $("li:nth-last-child(2)").css({background:"red"});
            $("li:nth-last-of-type(3)").css({background:"pink"});

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

上一篇:Node.js基础总结 下一篇:BFC的作用及原理
热门