节点的删除,获取和设置元素的属性

200 阅读1分钟
节点的删除

remove  删除整个节点 可以自己删除自己\

empty 清空节点内容\

detach():删除整个节点,保留元素的绑定事件、附加的数据\

empty remove 和 detach 执行完之后都有返回值,返回删除的节点**

替换节点 replaceWith\

clone 克隆节点

true复制事件处理,flase时反之false或者不传参数都不能实现复制事件 遍历子元素

children()方法可以用来获取元素的所有子元素   children获取不到元素会返回jq对象 document\

find 可以获取后代的元素 next()用于获取紧邻匹配元素之后的元素

有多个相同的元素 会返回一个紧邻匹配元素之后的元素的集合

获取和设置元素的属性:


        *{

            margin: 0%;

            padding: 0%;

        }

        .box{

            width: 340px;

            border: 1px solid blue;

            margin: 10px auto;

        }

        .box h1{

            height: 40px;

            color: #fff;

            padding-left: 15px;

            background-color: blue;

            font-size: 25px;

        }

        ul li{

            padding-left: 15px;

            list-style-type: none;

            line-height: 45px;

            border-bottom: 1px dashed #ccc;

        }

        ul li:last-child{

            border-bottom: none;

        }

    </style>

</head>

<body>

    <div class="box">

        <h1>

            祝福冬奥

        </h1>

        <ul>

          <li>贝克汉姆</li>

          <li>姚明</li>

          <li>张宏</li>

          <li>肖恩怀特</li>

      </ul>

      </div>

      <div><img src="" alt=""></div>

      <!-- <input type="radio" name="a">男

      <input type="radio" name="a">女 -->

      <input type="checkbox" id="lanqiu">篮球

      <input type="checkbox" id="zuqiu" >足球

      <h2></h2>

\


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

      <script>

        $('input').click(function(){

            console.log(1);

            /* prop是获取自身的属性 attr是获取元素自定义的属性 */

            console.log($(this).prop('checked'));

        })

\


        $('#lanqiu').click(function(){

            checkFn.call(this,'我热爱篮球',$('h3'))

        })

        $('#zuqiu').click(function(){

            checkFn.call(this,'我热爱足球',$('h3'))

        })

        function checkFn(){

            if($(this).prop('checked')){

               $('h2').append(aihao)

            }else{

                let t = obj.text();

                let n = t.replace(aihao,'')

                obj.text(n)

            }

        }

           /* 自定义属性 */

          /* 用 attr 来获取属性值 */

         console.log($('li:eq(0)').attr('data-a'));

         /* attr 来设置属性值 */

         $('li:eq(1)').attr('data-b','祝福中国滑冰队')

         /* 获取图片原有的属性 */

         /* console.log($('img').attr('alt')); */

         /* 设置图片的宽高 */

         $('img').attr('width','100px')

         $('img').attr('height','100px')

         /* 删除图片的属性 */

         $('img').click(function(){

             $(this).removeAttr('width')

             $(this).removeAttr('height')

          })

      </script>