(学习)document.getElementsByClassName()

根据:html中document.getElementsByClassName() 方法_徊忆羽菲的博客-优快云博客_document.getelementbyclassname()

  •  document.getElementsByClassName()方法返回文档中所有指定类名的元素集合,作为NodeList对象
  •  NodeList 对象代表一个有顺序的节点列表。 NodeList对象,我们可以通过节点列表中的节点索引号来访问列表中的节点(索引号从0开始)
  • 提示:你可以使用NodeList对象的length属性来确定指定类名的元素个数,并循环各个元素来获取你需要的那个元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>document.getElementsByClassName()的学习1</title>
</head>
<body>
<ul class="dongdongdong">
  <li class="child">meitiankaixin</li>
  <li class="child">xiaodong</li>
</ul>
<p>点击按钮修改第一个列表项的文本信息 (索引值为 0)。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var list = document.getElementsByClassName("dongdongdong")[0];
    console.log(document.getElementsByClassName("dongdongdong"),"获取类名");//dongdongdong,集合   HTMLCollection [ul.dongdongdong]
    console.log(document.getElementsByClassName("dongdongdong").length,"获取类名的长度");//   1
    console.log(document.getElementsByClassName("dongdongdong")[0],"获取类名的第0项");   //   dongdongdong 和他的子元素
    list.getElementsByClassName("child")[0].innerHTML = "shengrikuaile";
    console.log(list.getElementsByClassName("child"),"子元素的类名");  // 是两个child   HTMLCollection(2) [li.child, li.child]
    console.log(list.getElementsByClassName("child").length,"子元素的类名长度");  // 2
    console.log(list.getElementsByClassName("child")[0],"子元素的类名的第一项");  // meitiankaixin,不过被改成了shengrikuaile
}
</script>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>document.getElementsByClassName()的学习2</title>
<style>
div {
    border: 1px solid pink;
    margin: 5px;
}
</style>
</head>
<body>
<div id="发财发财">
  <p class="child">div 元素中第一个 class="child" 的 p 元素 (索引值为 0).</p>
  <p class="child">div 元素中第二个 class="child" 的 p 元素 (索引值为 1).</p>
  <p class="child">div 元素中第三个 class="child" 的 p 元素 (索引值为 2).</p>
</div>
<p>点击按钮为 div 元素中第二个 class="child" 的 p 元素添加背景颜色。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var x = document.getElementById("发财发财");
    x.getElementsByClassName("child")[1].style.backgroundColor = "yellow";
}
</script>
</body>
</html>

第一个代码// ==UserScript== // @name 点击弹窗 // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match https://study.enaea.edu.cn/* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @run-at document-end // @grant none // ==/UserScript== (function() { 'use strict'; window.setInterval(function(){ var v=document.getElementsByTagName('video'); for (var i=0;i<v.length;i++) { if(v[i].playbackRate!=3) {v[i].playbackRate = 3;} } },5000) window.setInterval(function(){ if($('button:contains(继续学习)').text()!='') $('button:contains(继续学习)')[0].click() },5000) // Your code here... })();第二个代码// ==UserScript== // @name 自动切换 // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match https://study.enaea.edu.cn/circleIndexRedirect.do?action=toNewMyClass&type=course&circleId=* // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @run-at document-end // @grant none // ==/UserScript== (function() { 'use strict'; window.setInterval(function(){ location.href = location.href; var progressValues=document.getElementsByClassName('progressvalue'); //获取所有课程进度值 var startbuttons=document.getElementsByClassName('golearn ablesky-colortip saveStuCourse');//获取开始学习按钮 var isEnd=true; //遍历所有进度,查看已学习课程是否都学至进度100% for (var i = 0; i < progressValues.length; i++) { if(progressValues[i].textContent!='0%' && progressValues[i].textContent!='100%'){ isEnd=false; break; } } //若已学完当前课程,切换下一集播放 if(isEnd){ for (var j = 0; j < progressValues.length; j++) { if(progressValues[j].textContent=='0%'){ startbuttons[j].click(); break; } } } },3*60*1000) // Your code here... })();给我
最新发布
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值