jquery通过索引值操作html元素的代码

eq() :返回带有被选元素的指定索引号的元素。注意:索引号从 0 开头,所以第一个元素的索引号是 0(不是 1)。

remove(): 移除被选元素,包括所有文本和子节点。

html():返回或设置被选元素的内容。

attr():设置或返回被选元素的属性值。如attr("class", "aaa time")

prepend():被选元素的开头(仍位于内部)插入指定内容。

animationend 事件:在 CSS 动画完成后触发。

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <style>
 .aaa {
 color: red;
 animation: mymove 4s
 }
 
 @keyframes mymove {
 25% {
 font-size: 30px;
 color: chartreuse
 }
 
 50% {
 color: blue
 }
 }
 </style>
</head>
 
<body>
 <div id="person">
 <div><span id="t1" class="time">小明</span></div>
 <div id="t2"><span class="time">小涛</span></div>
 <div><span class="time">小花</span></div>
 <div><span class="time">小刚</span></div>
 <div><span class="time">小狗</span></div>
 <div><span class="time">小毛</span></div>
 </div>
 <script src="jquery-1.7.1.min.js"></script>
 <script>
 // 1.移除页面上class=time的索引值为2的元素
 $(".time").eq(2).remove()
 // 2.设置页面上class=time的索引值为0的元素的内容为9999
 $(".time").eq(0).html(9999)
 // 3.设置页面上class=time的索引值为1的元素的class="aaa time"
 $(".time").eq(1).attr("class", "aaa time")
 // 4.隐藏页面上class=time的索引值为4的元素
 $(".time").eq(4).hide()
 // 5.往id=person的标签中第一行插入html
 var name = "maidu"
 $("#person").prepend("<div><span class='time' id=" + name + ">" + name + "</span></div>")
 // 6.监听动画,当动画结束后去掉class=aaa
 document.getElementsByClassName("time")[2].addEventListener('animationend', function (e) {
 this.classList.remove("aaa")
 })
 
 </script>
</body>
 
</html>
作者:maidu_xbd原文地址:https://blog.csdn.net/maidu_xbd/article/details/105694784

%s 个评论

要回复文章请先登录注册