Web前端-关于jQuerry

jQuery-The write less,do more,jacascript library

非常方便的js库,封装了许多有用的功能。

1.jq与js对象之间的转换

    <script>
        // js对象转换为jq对象
        var v=document.getElementById("div1");//js对象
        var $jqv=$(v);//jq对象。变量名字前边加$是为了标明是jq对象


        //jq对象->js对象
        var $jqv=$("#div1"); //类选择器获得jq对象
        var jsv=$jqv.get(0);//第一种方法
        // var jsv=$jqv[0];//第二张方法
    </script>

2.jq的选择器(列举了几个最常用的,可看jQuerry参考手册)

    <script>
        // 1.元素选择器
        $("#div1") //id为div1的元素
        $(".group")//class为group的元素组合
        $("p")//所有<p>元素
        $("p.group")//id为group的<p>元素
        // 2.css选择器
        $("p").css("background-color","red"); //设置css样式
        // 3.属性选择器
        $("[href]") //选取带有href属性的元素
        $("[href='#']")
        $("[href!='#']")
        $("[href$='.png']") //选取href属性以png结尾的元素
        // 4.层级选择器
        $("p > span")  // 子元素选择器: $(“选择器1” > “选择器2”) 选取p的子标签中为span的标签
        $("p span") // 子孙选择器: $(“选择器1” (空格) “选择器2”)选取p的子孙标签中为span的标签
        $("p + span")// 兄弟选择器(同级选择): $(“选择器1” + “选择器2”) 选取p后边的第一个为span的标签
        $("p ~ span"") // 所有兄弟选择器(同级选择): $(“选择器1” ~“选择器2”) 选取p后边的所有为span的标签
        // 5.表单选择器
        $(":input")//获取input元素
        $(":text") //type类型为text的元素 下同
        $(":password")
        $(":file")
        // 6.基本过滤选择器
        $("p:first")//选取页面第一个first元素
        $("p:last")
        $("p:last")
        $("p:not(.group)") //p的类不是group的元素
        $("p:even") //选取索引是偶数的元素(从0开始,下同)
        $("p:odd")
        $("p:eq(index)") //索引为index的元素
        $("p:gt(index)") //索引大于index的元素
        $("p:lt(index)") //索引小于index的元素

        $(":animated")//选取正在执行动画的元素
        $("input:focus") //输入获得焦点的元素
        $("option:selected") //被选择的元素
        //7.内容过滤器
        $("div:contains('获取div中含有该内容的元素')")
        $("div:empty") //div中空的元素
        $("div:has(#div1)")//div内id为div1的元素
    </script>

3.省市联动--jq的遍历&添加子节点

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="jquery-3.3.1.js"></script>
    <script>
        var allcities=[
            ["济南市","青岛市","滨州市"],
            ["贵林","六盘水","毕节"],
            ["石家庄","秦皇岛","保定"],
        ]
        $(function () {
            // 绑定change时间,一改变就触发这个函数
            $("#provinces").change(function(){
                var cities=allcities[this.value];
                //首先清空之前的城市内容
                $("#cities").empty();
                //遍历数组。依次添加到select中。i为下标值,n为内容
                $(cities).each(function(i,n){
                    $("#cities").append("<option>"+n+"</option>");
                })
            })
        })
    </script>
</head>
<body>
<div>
    <label>省份</label>
    <select  id="provinces" >
        <option value="-1">--请选择</option>
        <option value="0">山东省</option>
        <option value="1">贵州省</option>
        <option value="2">辽宁省</option>
    </select>
    <select id="cities">

    </select>
</div>
</div>
</body>
</html>

 4.表格的全选操作和隔行换色

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表格的全选操作and表格的隔行换色操作</title>
    <script type="text/javascript" src="jquery-3.3.1.js"></script>
    <script>
        $(function(){
            $("#topcheck").click(function(){
                // topcheck 点击后,将otherchecks checked属性设置为this.checked
                $(".otherchecks").prop("checked",this.checked);
            })
            // 页面加载完成后,改变表格的颜色
            $("#table1 tr:even:gt(0)").css("background-color","yellow");
            $("#table1 tr:odd").css("background-color","wheat");
        })
    </script>
</head>
<body >
<table border="2" id="table1" >
    <tr>
        <td><input type="checkbox" id="topcheck" ></td>
        <td>商品名称</td>
        <td>商品种类</td>
    </tr>
    <tr>
        <td><input type="checkbox"  class="otherchecks"></td>
        <td>华为p30</td>
        <td>手机</td>
    </tr>
    <tr>
        <td><input type="checkbox"  class="otherchecks"></td>
        <td>macbookpro</td>
        <td>电脑</td>
    </tr>
    <tr>
        <td><input type="checkbox" class="otherchecks"></td>
        <td>AppleTv</td>
        <td>电视</td>
    </tr>
    <tr>
        <td><input type="checkbox"  class="otherchecks"></td>
        <td>iphoneX</td>
        <td>手机</td>
    </tr>
    <tr>
        <td><input type="checkbox"  class="otherchecks"></td>
        <td>sony1000XM3</td>
        <td>耳机</td>
    </tr>
    <tr>
        <td><input type="checkbox"  class="otherchecks"></td>
        <td>kindle</td>
        <td>电子书</td>
    </tr>
</table>
</body>
</html>

 

posted @ 2019-04-17 20:45  nlw  阅读(362)  评论(0编辑  收藏  举报