三、jQuery的API (三)- 文档筛选、文档处理模块

一、文档筛选模块 – DOM查询

	
	+++ 筛选
			在jQuery对象内部的元素中找出部分匹配的元素,并封装为新的jQuery对象返回。
	
	+++ 查找
			由jQuery对象内部的元素查找其他的子孙/兄弟/父母元素,并封装为新的jQuery对象返回

		
1.1 过滤

在一个jQuery对象中找出所有的匹配元素,并封装为新的jQuery实例对象返回

	
		筛选
			first()   筛选出第一个元素
			last() 	  筛选出最后一个元素
			eq(index|-index)  筛选出指定索引值元素
			
			filter(selector|ele) 在jQuery对象内部的元素中筛选出与指定表达式匹配的元素
			not(selector|ele)    在jQuery对象内部的元素中删除与指定表达式匹配的元素
			
			slice(start,[end]) 截取jQuery对象内部的元素
			
			has(selector|ele)  在jQuery对象内部的元素中筛选出包含指定后代的元素

			hasClass(class)    判断元素是否有class属性,返回truefalse

1)first()、last() 、eq(index|-index)
<body>

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <span>5</span>
    </ul>

    <ul>
        <li>sss</li>
        <li>222</li>
    </ul>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");
    
    //ul下li标签的第一个
    $("ul>li").first().css("background-color","red")

    //ul下li标签的最后一个
    $("ul>li").last().css("background-color","red")

    //ul下li标签的第二个
    $("ul>li").eq(1).css("background-color","red")


</script>

在这里插入图片描述

>>>>>> 注意:如果匹配元素有多个,则可能会依次匹配多个返回集合
>>>>>> 注意:筛选与查找返回jQuery对象,可以流式过滤
2)filter(selector|ele) 、not(selector|ele)
>>>>>> 基础使用
<body>

    <ul>
        <li title="a"><span>3</span></li>
        <li title="box">2</li>
        <li><span>3</span></li>
        <li title="box">4</li>
        <span>5</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");

    //ul下li标签中title=box的元素
    $("ul>li").filter("[title=box]").css("background-color","red");

    //ul下li标签中title!=box的元素
    //方式一
    $("ul>li").not("[title=box]").css("background-color","blue");
    //方式二
    $("ul>li").filter("[title!=box]").css("background-color","blue");

</script>

在这里插入图片描述

>>>>>> 流式使用
<body>

    <ul>
        <li title="a"><span>3</span></li>
        <li title="box">2</li>
        <li><span>3</span></li>
        <li title="box">4</li>
        <span>5</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");

    //ul下li标签中有title属性但是title!=box的元素
    $("ul>li").filter("[title]").not("[title=box]").css("background-color","blue");

</script>

在这里插入图片描述

3)slice(start,[end]) 截取
<body>

    <ul>
        <li title="a"><span>3</span></li>
        <li title="box">2</li>
        <li><span>3</span></li>
        <li title="box">4</li>
        <span>5</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");


    //截取集合中第一个元素
    $("ul>li").slice(0,1).css("background-color","blue")

    //截取集合元素,从第三个开始
    $("ul>li").slice(2).css("background-color","red")



</script>

在这里插入图片描述

4)has(selector|ele)
<body>

    <ul>
        <li title="a"><span>3</span></li>
        <li title="box">2</li>
        <li><span>3</span></li>
        <li title="box">4</li>
        <span>5</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");


    //ul下li元素中含有span后台的元素
    $("ul>li").has("span").css("background-color","blue")



</script>

在这里插入图片描述

5)hasClass(class)
<body>

    <ul>
        <li><span>3</span></li>
        <li>2</li>
        <li class="ccc"><span>3</span></li>
        <li>4</li>
        <span>5</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    $lis=$("ul>li");

    $("ul>li").each(function(){

        var flag=$(this).hasClass("ccc");
        if(flag){
            $(this).css("background-color","red")
        }
    })



</script>

在这里插入图片描述

1.2 查找

由jQuery对象内部的元素查找其他的子孙/兄弟/父母元素,并封装为新的jQuery对象返回


		查找
			
			+++ 查找父元素
					parent([expr])  查找所有匹配元素的唯一父元素
					parents([expr]) 查找所有匹配元素的所有祖先元素
					offsetParent()  查找第一个匹配元素用于定位的父节点。

			+++ 查找子元素
				
					children([selector])  查找所有匹配元素的的子元素(不含后代元素)
					find(selector)		  查找所有匹配元素的后代元素


			+++ 查找兄弟元素
					prev([expr])    查找所有匹配元素的上一个兄弟元素
					next([expr]) 	查找所有匹配元素的下一个兄弟元素
		
					prevall([expr]) 查找所有匹配元素的上面的所有兄弟元素
					nextall([expr]) 查找所有匹配元素的下面的所有的兄弟元素
					siblings([expr]) 查找所有匹配元素之后所有的同辈元素 





1)查找父元素
>>>>>> 查找父元素

<body>

    <div >
        <ul>
            <li><span>3</span></li>
            <li>2</li>
            <li><span>3</span></li>
            <li id="cc">4</li>
            <span>5</span>
            <span>6</span>
        </ul>
        <span>11</span><br>
        <span>22</span><br>
    </div>

</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    //ul标签的父标签
    $("#cc").parent().css("background-color","red")

</script>

在这里插入图片描述

>>>>>> 查找所有父辈元素
<body>


    <div >
        <ul>
            <li><span>3</span></li>
            <li>2</li>
            <li><span>3</span></li>
            <li id="cc">4</li>
            <span>5</span>
            <span>6</span>
        </ul>
        <span>11</span><br>
        <span>22</span><br>
    </div>

</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    //ul标签的父辈标签
    $("#cc").parents("div").css("background-color","red")

</script>

在这里插入图片描述

2)查找子元素
<body>

    <ul>
        <li><span>3</span></li>
        <li>2</li>
        <li><span>3</span></li>
        <li>4</li>
        <span>5</span>
        <span>6</span>
    </ul>


</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    //ul下所有的子元素
    $("ul").children().css("color","pink")

    //ul标签内的第2个span子元素
    $("ul").children("span:eq(1)").css("background-color","red")

    //ul标签内的第2个span后代元素
    $("ul").find("span:eq(1)").css("background-color","blue")


</script>

在这里插入图片描述

3)查找兄弟元素
<body>


    <div >
        <ul>
            <li><span>3</span></li>
            <li>2</li>
            <li><span>3</span></li>
            <li id="cc">4</li>
            <span>5</span>
            <span>6</span>
        </ul>
        <span>11</span><br>
        <span>22</span><br>
    </div>

</body>

<script type="text/javascript" src="js/jquery-1.12.4.js" ></script>
<script>

    //id为cc的所有兄弟标签
    $("#cc").siblings().css("background-color","red")

    // id为cc的前面的兄弟标签
    $("#cc").prev().css("background-color","red")
    // id为cc的前面的所有兄弟标签
    $("#cc").prevAll().css("background-color","red")


    // id为cc的后面的兄弟标签
    $("#cc").next().css("background-color","red")
    // id为cc的后面的所有兄弟标签
    $("#cc").nextAll().css("background-color","red")


</script>
>>>>>> 案例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
    <form>
        你爱好的运动是?<input type="checkbox" id="checkAllBox"> 全选/全不选 <br>
        <input type="checkbox" name="items" value="足球">足球</input>
        <input type="checkbox" name="items" value="篮球">篮球</input>
        <input type="checkbox" name="items" value="黑球">黑球</input>
        <input type="checkbox" name="items" value="白球">白球</input> <br>

        <input type="button" id="checkAllBtn"  value="全  选">
        <input type="button" id="checkNoBtn"  value="全不选">
        <input type="button" id="checkReoBtn"  value="反选">
        <input type="button" id="sendBtn"  value="提交">

    </form>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

    var $lo=$(":checkbox").filter("[name=items]");


    //全选/全不选
    $("#checkAllBox").click(function(){
        
        if($("#checkAllBox").prop("checked")){
            $lo.prop("checked",true);
        }else{
            $lo.prop("checked",false);
        }  
    })


    //全选
    $("#checkAllBtn").click(function(){
            $lo.prop("checked",true);
    })
  
    
    //全不选
    $("#checkNoBtn").click(function(){
            $lo.prop("checked",false);
    })


     //反选
     $("#checkReoBtn").click(function(){
            $lo.each(function(){
                console.log(!$(this).prop("checked"))
                $(this).prop("checked",!$(this).prop("checked"))
            })
    })


     //提交
     $("#sendBtn").click(function(){
        var arr=new Array();
        $lo.each(function(){
            if($(this).prop("checked")){
                arr.push(this)
            }
        })

        console.log(arr)
    })


</script>
</html>

在这里插入图片描述

二、文档处理模块 - DOM增删改(CUD)

		
		+++1) 插入后部(内部插入)
					append(content|fn)  在元素内部追加内容
					appendTo(selector) 	将元素追加到某个元素的内部
					
					prepend(content|fn) 在元素内部前置内容
					prependTo(content)  将元素前置到某个元素的内部
				
		 	  2) 外部插入(插入后面)
					after(content|fn)   在元素后面添加内容
					insertAfter(selector) 
	
					before(content|fn)  在元素前面添加内容
					insertBefore(selector) 

				

		+++ 删除
				empty() 	 删除元素中的内部内容(掏空,自己还在)
				remove([selector]) 	 删除自身/删除匹配的元素
					
							empty()和remove()的区别在于:
											1、empty()是清空内部元素。
											2、remove()是吧自己也删除了。



		+++replaceWith(content|fn)  将匹配的元素替换为指定的内容
				
				replaceAll(selector)     将匹配的元素替换掉指定的元素
											注意:相当于将匹配的元素移到指定的元素的位置。

1.1 增
1)内部插入
>>>>>> 内部追加(在元素内部的后面添加)
<body>
    
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

    //在ul元素的内部追加内容
    $("ul").append("<span>追加的内容1</span>")

    //将内容追加到元素的内部
    $("<span>追加的内容2</span>").appendTo("ul")

</script>

在这里插入图片描述

>>>>>> 内部前置(在元素内部的前面添加)
<body>
    
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

 

    //在ul元素的内部前置内容
    $("ul").prepend("<span>前置的内容1</span>")

    //将内容前置到元素的内部
    $("<span>前置的内容2</span>").prependTo("ul")   
    
</script>

在这里插入图片描述

2)外部插入
>>>>>> 外部追加(在元素的后面添加)
<body>
    
    <ul>
        <li>1</li>
        <li class="sss">2</li>
        <li>3</li>
    </ul>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

    
    //在元素后面追加内容
    $(".sss").after("<span>在元素后面添加的内容1<span>")

    //将内容追加到元素后面
    $("<span>在元素后面添加的内容2<span>").insertAfter(".sss")

</script>

在这里插入图片描述

>>>>>> 外部前置(在元素的前面添加)
<body>
    
    <ul>
        <li>1</li>
        <li class="sss">2</li>
        <li>3</li>
    </ul>
</body>

<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

    
    //在元素后面追加内容
    $(".sss").before("<span>在元素前面添加的内容1<span>")

    //将内容追加到元素后面
    $("<span>在元素前面添加的内容2<span>").insertBefore(".sss")

</script>

在这里插入图片描述

1.2 删
1)empty() 删除元素内部元素
<body>

    <div>123<span>456<span></div>

    
</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

        //删除span内部的内容,但是span标签还存在
        $("span").empty();
</script>

在这里插入图片描述

2)remove([selector]) 删除指定的元素
>>>>>> remove() 删除元素
<body>

    <div>
        xxxxxxxxxxxxx
        <span>1</span>
        <span class="tt">2</span>
        <span>3</span>
        <span>4</span>
        <span>5</span>

    </div>

    
</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>
        //删除所有匹配的元素
        $("span").remove();
</script>

在这里插入图片描述

>>>>>> remove(selector) 删除指定的元素
<body>

    <div>
        xxxxxxxxxxxxx
        <span>1</span>
        <span class="tt">2</span>
        <span>3</span>
        <span>4</span>
        <span>5</span>

    </div>

    
</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>
        //删除指定的元素
        $("span").remove(".tt");
</script>

在这里插入图片描述

1.3 改
1)replaceWith(content|fn) 将匹配的元素替换为指定的内容
<body>

    <div>
        xxxxxxxxxxxxx
        <span>1</span>
        <span class="tt">2</span>
        <span>3</span>
        <span>4</span>
        <span>5</span>

    </div>

    
</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

        //将选中的替换为指定的内容
        $(".tt").replaceWith("替换的内容")

</script>

在这里插入图片描述

2)replaceAll(selector) 将匹配的元素替换掉指定的元素
<body>
    <span class="cc">xxxxxxxx</span>
    <div>
        xxxxxxxxxxxxx
        <span>1</span>
        <span class="tt">2</span>
        <span>3</span>
        <span>4</span>
        <span>5</span>

    </div>

    
</body>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

        //将匹配的元素替换到指定的元素。
        $(".tt").replaceAll(".cc")

</script>

在这里插入图片描述

三、案例
>>>>>> 案例
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


    <style>

        table:first-of-type{
            width:400px;
            margin:30px auto;
            border:1px solid red;
        }

        table:last-of-type{
            width:300px;
            margin:50px auto;

            border:2px solid black;
            padding:20px;
        }

        td{
            padding:2px
        }

        .cc{
            padding-top:10px;
            text-align: center;
        }

        .cc input{
            width:80px;
            height:30px;
        }

    </style>



<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>

        function removeTr(obj){
            $(obj).parent().parent().remove();
        }

        $(function(){
            //新增
            $(".btn").click(function(){
                            
                //判断非空
                var str='';
                $(":text").each(function(){

                    var key=$(this).parent().prev().html();
                    var value=$(this).val();
                    if( value==''){
                        str=str+ key+"为空!!";
                    }
                })

                if(str !=''){
                    alert(str);
                    return;
                }

                var name =$(":text").eq(0).val();
                var age =$(":text").eq(1).val();
                var email =$(":text").eq(2).val();
                var remark =$(":text").eq(3).val();

                $("table").first().find("tr").last().after(
                    " <tr><td>"+name+"</td><td>"+age+"</td><td>"+email+"</td><td>"+remark+"</td> <th><a href='javascript:;' οnclick='removeTr(this)''>删除</a></th></tr>"
                )

                //置空
                $(":text").prop("value","");

             })
        })
      
      
</script>
</head>
<body>

    <table border="1" >
        <tr>
            <th>Name</th>
            <th>age</th>
            <th>Email</th>
            <th>remark</th>
            <th></th>
        </tr>
        <tr>
            <td>王帅</td>
            <td>12</td>
            <td>554030344@qq.com</td>
            <td>xxx</td>
            <th><a href="javascript:;" onclick="removeTr(this)">删除</a></th>
        </tr>
        <tr>
            <td>王帅</td>
            <td>12</td>
            <td>554030344@qq.com</td>
            <td>xxx</td>
            <th><a href="javascript:;" onclick="removeTr(this)">删除</a></th>
        </tr>
    </table>
    
    <table>
        <caption>添加新员工</caption>
        <tr>
            <td>name</td>
            <td><input type="text"></td>
        </tr>
        <tr>
            <td>age</td>
            <td><input type="text"></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input type="text"></td>
        </tr>
        <tr>
            <td>remark</td>
            <td><input type="text"></td>
        </tr>
        <tr>
            <td colspan="2" class="cc"><input type="submit" value="添加员工" class="btn"></td>
        </tr>
    </table>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值