jQuery: 整理5---删除元素和遍历元素

1. 删除元素

   span{
       color: white;
       padding: 8px;
       margin: 5px;
       float: left;
    }
    .green {
       background-color: green;
    }
    .blue {
       background-color: blue;
    }
   <span class="green">green</span>
   <span class="blue">blue</span>
   <span class="green">333</span>
   <span class="blue">444</span>

1. 使用  remove()   ->  删除元素及其子元素,标签和内容一起删除

   指定元素.remove()

$(".green").remove()

2.  empty()   ->  清空元素内容, 保留标签

 $(".blue").empty()

2. 遍历元素

span{
  color: white;
  padding: 8px;
  margin: 5px;
  float: left;
 }
.green {
  background-color: green;
}
.blue {
  background-color: blue;
}

<h3>遍历元素</h3>
<span class="green">green</span>
<span class="green">blue</span>
<span class="green">333</span>
<span class="green">444</span>

each():

   $(selector).each(function(index,element))

       // 参数function为遍历时的回调函数

        // index为遍历元素的序列号,从0开始

        // element是当前的元素,此时是dom元素

 // 获取指定元素并遍历
 $(".green").each(function(index, element) {
      console.log(index)
      console.log(element)
 })

相关推荐

  1. jquery删除一个页面元素动画特效

    2023-12-26 11:28:05       22 阅读
  2. jQuery改变元素的方法

    2023-12-26 11:28:05       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-26 11:28:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-26 11:28:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-26 11:28:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-26 11:28:05       20 阅读

热门阅读

  1. 随记-SQLAlchemy ORM 梳理

    2023-12-26 11:28:05       38 阅读
  2. python区块链简单模拟【04】

    2023-12-26 11:28:05       33 阅读
  3. Linux网卡配置

    2023-12-26 11:28:05       35 阅读
  4. LeetCode 22 括号生成

    2023-12-26 11:28:05       41 阅读
  5. C++基础-多态详解

    2023-12-26 11:28:05       39 阅读