jQuery方法小记

简介: jQuery方法小记

jQuery


CDN

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>


clone()

//复制
        $("button:eq(0)").click(function() {
          $("ol").append($("li").clone());//在最后添加 复制的所有元素
        });
        $("button:eq(0)").hover(
          function() {
            $("ol").css("color", "pink");
          },
          function() {
            $("ol").css("color", "black");
          });


append()

//增加    

        $("button:eq(1)").click(function() {
        const lis_r = $("<li>jQuery replaceWith()方法</li>");
        const lis_c = $("<li>jQuery clone()方法</li>");
          if ($("li").html() == "jQuery clone()方法") {
            $("ol").append(lis_c);
          } else {
            $("ol").append(lis_r);
          }
        });


remove(),empty()

        $("button:eq(2)").dblclick(function() {
          tips();
          $("ol").empty();//双击清空
        });
        $("button:eq(2)").click(function() {
          $("li:last").remove();//删除最后一个元素
        }); 
        function tips() {
          if ($("ol").text() == "") {
            alert("请先增加元素!");
          };
        };


replaceWith()

//替换
        $("button:eq(3)").click(function() {
        const lis_r = $("<li>jQuery replaceWith()方法</li>");
        const lis_c = $("<li>jQuery clone()方法</li>");
          if ($("li").html() == "jQuery clone()方法") {
            $("li").replaceWith(lis_r); //li元素替换为 lis_r
          } else {
            $("li").replaceWith(lis_c);
          }
        });
        $("button:eq(4)").click(function() {
          $("button:eq(2)").unbind("click");
        });


全部

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">     
      $(document).ready(function() {
        //复制
        $("button:eq(0)").click(function() {
          $("ol").append($("li").clone());
        });
        $("button:eq(0)").hover(
          function() {
            $("ol").css("color", "pink");
          },
          function() {
            $("ol").css("color", "black");
          });
        //增加    

        $("button:eq(1)").click(function() {
        const lis_r = $("<li>jQuery replaceWith()方法</li>");
        const lis_c = $("<li>jQuery clone()方法</li>");
          if ($("li").html() == "jQuery clone()方法") {
            $("ol").append(lis_c);
          } else {
            $("ol").append(lis_r);
          }
        });
        //删除
        $("button:eq(2)").dblclick(function() {
          tips();
          $("ol").empty();
        });
        $("button:eq(2)").click(function() {
          $("li:last").remove();
        });

        function tips() {
          if ($("ol").text() == "") {
            alert("请先增加元素!");
          };
        };
        //替换
        $("button:eq(3)").click(function() {
        const lis_r = $("<li>jQuery replaceWith()方法</li>");
        const lis_c = $("<li>jQuery clone()方法</li>");
          if ($("li").html() == "jQuery clone()方法") {
            $("li").replaceWith(lis_r);
          } else {
            $("li").replaceWith(lis_c);
          }
        });
        $("button:eq(4)").click(function() {
          $("button:eq(2)").unbind("click");
        });
      });
    </script>
    <style type="text/css">
      /* 从1开始 */
      button:nth-child(5){
        background: #245123;
      }
    </style>
  </head>
  <body>
    <button type="button">复制</button>
    <button type="button">增加</button>
    <button type="button">删除</button>
    <button type="button">替换</button>
    <button type="button">失效</button>
    <ol>
      <li>jQuery clone()方法</li>
    </ol>
  </body>
</html>

相关文章
|
4月前
|
JavaScript
jQuery追加节点方法 和height方法与width方法
jQuery追加节点方法 和height方法与width方法
|
4月前
|
JavaScript 前端开发
调用jQuery的animate()方法无法移动的问题
调用jQuery的animate()方法无法移动的问题
|
4月前
|
JavaScript 前端开发 UED
jQuery 自动刷新页面但不闪烁的实现方法
jQuery 自动刷新页面但不闪烁的实现方法
|
5月前
|
JavaScript 前端开发
jQuery学习(十二)—jQuery中对象的查找方法总结
jQuery学习(十二)—jQuery中对象的查找方法总结
|
5月前
|
JavaScript
jQuery学习(九)—常用的包裹方法
jQuery学习(九)—常用的包裹方法
|
5月前
|
JavaScript
jQuery学习(七)— append方法与appendTo方法
jQuery学习(七)— append方法与appendTo方法
|
5月前
|
JavaScript
jQuery学习(八)—before方法、after方法、insertBefore方法、insertAfter方法
jQuery学习(八)—before方法、after方法、insertBefore方法、insertAfter方法
|
26天前
|
JavaScript Serverless
jquery attr()方法
jquery attr()方法
15 0
|
6天前
|
JavaScript
jquery获取子元素的一些方法
jquery获取子元素的一些方法
|
9天前
|
JavaScript
jQuery遍历节点的方法
jQuery遍历节点的方法
8 0