• 技术文章 >web前端 >js教程

    jquery基本选择器practice的实例详解

    黄舟黄舟2017-09-30 11:19:00原创768
    下面小编就为大家带来一篇jquery之基本选择器practice(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    一、在输入框中输入数字,点击按钮,实现对应事件的功能。

    html代码:


    <input id="txt1" type="text" value="2" />
    <input id="Button5" type="button" value="改变大于N的行背景为绿色" />

    jQuery代码:


    //改变大于N的行背景为绿色
          $("#Button5").click(function () {
            //获取到ID为txt1的输入框的文本值
            var num = $("#txt1").val();
            //tr的行的下标从0开始,故现实中的数字应该减一
            num = num - 1;
            $("tr:gt("+num+")").css("background-color", "green");
          });

    二、点击每一个蓝色线框中的p时,改变它后面紧邻的元素的背景为green

    html代码:


    <p class="mainbox">
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
        <p>6</p>
        <p>7</p>
        <p>8</p>
        <p>9</p>
      </p>

    jQuery代码:


     $("p").click(function () {
             $(this).next("p").css("background-color","green");
           });

    页面加载完毕后,让所有数字为奇数的p的字体颜色改为blue


    //2.页面加载完毕后,让所有数字为奇数的p的字体颜色该为blue
          //$("p.mainbox>p:even").css("color", "blue");
          for (var i = 0; i < $(".mainbox>p").length; i++) {
            //获取到每p的集合
            var valu = $(".mainbox>p");
            //获取到每一个p中的文本内容
            var txt = $(valu[i]).text();
            //将string转换为int
            value = parseInt(txt);
            //取模进行奇偶判断
            if (value%2!=0) {
              $(valu[i]).css("color", "blue");
            }
          }

    三、编写javascript代码,完成如下功能要求:

    实现全选、反选、全不选功能

    大前端成长进阶课程:进入学习

    HTML代码:


    <tr>
            <td>
              <label>
                <input type="radio" name="selectMode" id="selectAll" />全选
              </label>
              <label>
                <input type="radio" name="selectMode" id="selectNotAll" />全不选
              </label>
              <label>
                <input type="radio" name="selectMode" id="selectRevorse" />反选
              </label>
            </td>
          </tr>
          <tr>
            <td>
              <label>
                <input type="checkbox" id="Checkbox3" />刘德华
              </label>
              <label>
                <input type="checkbox" id="Checkbox4" />张学友
              </label>
              <label>
                <input type="checkbox" id="Checkbox5" />孙燕姿
              </label>
              <label>
                <input type="checkbox" id="Checkbox6" />刘欢
              </label>
            </td>
          </tr>

    jQuery代码:


    $(function () {
          //全选
          //方法1:
          $("#selectAll").click(function () {
            $("#Checkbox3,#Checkbox4,#Checkbox5,#Checkbox6").prop("checked",true);
          });
          //方法2:
          $("#selectAll").click(function () {
            //:checkbox--选取所有类型为checkbox的input标签
            $(":checkbox").prop("checked", true);
          });
          //全不选
          $("#selectNotAll").click(function () {
            $(":checkbox").prop("checked", false);
          });
          //反选方法1:
          $("#selectRevorse").click(function () {
            $(":checkbox").each(function () {
              $(this).prop("checked", !$(this).prop("checked"));
            });
          });
          //反选方法二2:
          $("#selectRevorse").click(function () {
            $("input[type=checked]").each(function (i, n) {
              n.checked = !n.checked;
            });
          });
          //反选方法3:
          $("#selectRevorse").click(function () {
            var $bob = $("input[type=checked]");
            for (var i = 0; i < $bob.length; i++) {
              if ($bob[i].checked == true) {
                $bob[i].checked == false;
              }
              else {
                $bob[i].checked == true;
              }
            }
          });
        });

    四、 将所有p标记下的儿子p前景色改为red

    将所有p标记的孙子span前景色改为green

    将i的爷爷的前景色改为Orange

    HTML代码:


    <p>
        <span>七大洲有哪些:大米粥、小米粥、绿豆粥、八宝粥... ...</span>
        <p>
          <span>中国四大发明时什么:油盐酱醋</span>
        </p>
        <p>
          我拿什么拯救你,<span>我的<i>瞌睡虫</i></span>
    
        </p>
      </p>

    jQuery代码:


    $(function () {
          //将所有p标记下的儿子p前景色改为red
          $("#Button1").click(function () {
            $("p>p").css("color","red");
          });
          //将所有p标记的孙子span前景色改为green
          $("#Button2").click(function () {
            $("p").children().children().css("color","green");
          });
          //将i的的爷爷的前景色改为Orange
          $("#Button3").click(function () {
            $("i").parent().parent().css("color","orange");
          });
        });

    五、请编写javascript代码,完成如下功能要求:

    每隔1秒,让所有的数字逆时针旋转

    效果如下:

    HTML代码:


    <p class="box">
        <table id="table1" class="mytable">
          <tr>
            <td>
              <label id="Label1">
                1
              </label>
            </td>
            <td>
              <label id="Label2">
                2
              </label>
            </td>

    jQuery代码:


    $(function () {
          window.setInterval(fun, 1000);
        });
        //方法一:
        function fun() {
          $("#table1 label").each(function (i, n) {
            //获取到当前label的文本值
            var $item = $(n).text();
            //将其转换为int型
            $item = parseInt($item);        
            if ($item == 8) {
              //给当前label赋值
              $(n).text("1");
            }
            else {
              //给当前label赋值
              $(n).text($item+1);
            }
          });
        };
        //方法二:
        function fun2() {
          $("#table1 label").each(function () {
            var n = $(this).text();
            n++;
            if (n > 8) {
              n = 1;
            }
            this.textContent = n;
            //$(this).text() = n;
          });
        }

    以上就是jquery基本选择器practice的实例详解的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    快捷开发Web应用及小程序:点击使用

    支持亿级表,高并发,自动生成可视化后台。

    专题推荐:practice jquery 选择器
    上一篇:jQuery表单验证的实例代码 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 聊聊Node中的异步实现与事件驱动• Node.js中怎么使用Redis?原来这么简单!• 深入聊聊前端限制用户截图的脑洞• JavaScript总结分享之闭包• 什么是RPC?聊聊node中怎么实现 RPC 通信
    1/1

    PHP中文网