self_example/java_web/JavaScript/html/15-HTML Element对象.html

89 lines
2.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>14-DOM</title>
</head>
<body>
<img id="light" src="../resource/off.gif"><br>
<div class="cls" id="timer">3秒后跳转</div>
<br>
<div class="cls">传智教育</div>
<br>
<div class="cls">黑马程序员</div>
<br>
<input type="checkbox" name="hubby"> 电影
<input type="checkbox" name="hubby"> 旅游
<input type="checkbox" name="hubby"> 游戏
<br>
<script>
//通过document对象获取element对象
/*
* getElementById()根据id属性值获取返回单个Element对象
* `getElementsByTagName()`根据标签名称获取返回Element对象数组
* `getElementsByName()`根据name属性值获取返回Element对象数组
* `getElementsByClassName()`根据class属性值获取返回Element对象数组
*
*
* */
//定量跳转的DOM实现
i = 3
setInterval(function () {
if (i == 0) {
location.href = "https://www.baidu.com"
}
var timer = document.getElementById("timer");
timer.innerHTML = i + "秒后跳转";
i--;
}, 1000)
//1.getElementById("light")
// var img = document.getElementById("light");
// // alert(img)
// img.src="../resource/on.gif";
// //2.getElementsByTagName():根据标签名称获取返回Element对象数组
// var divs = document.getElementsByTagName("div");
// //style:设置元素的css样式
// // innerHTML:设置元素内容
//
// // //alert(divs.length)
// // for (let i = 0; i < divs.length; i++) {
// // // divs[i].style.color='red';
// // divs[i].innerHTML="呵呵"
// // }
//
// //
// //
// // //3.getElementsByName()`根据name属性值获取返回Element对象数组
// var hubbys = document.getElementsByName("hubby");
// // // alert(hubbys.length)
// for (let i = 0; i < hubbys.length; i++) {
// // alert(hubbys[i])
// hubbys[i].checked=true;
// }
// //
// //
// // //4.getElementsByClassName()`根据class属性值获取返回Element对象数组
// // var clss = document.getElementsByClassName("cls");
// // for (let i = 0; i < clss.length; i++) {
// // alert(clss[i])
// // }
</script>
</body>
</html>