通过id找到标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
var userName = document.getElementById("userName");
//获取标签属性
console.log(userName.type);
//获取标签内容
console.log(userName.value);
var passWord = document.getElementById("passWord");
console.log(passWord.type);
console.log(passWord.value);
}
</script>
</head>
<body>
<input type="text" placeholder="请输入用户名" id="userName"><br/>
<input type="password" placeholder="请输入密码" id="passWord"><br/>
<button onclick="f()">确认</button>
</body>
</html>
通过标签名来获取标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
var inputTags = document.getElementsByTagName("input");
for (var i = 0; i < inputTags.length; i++) {
var inputTag = inputTags[i];
console.log(inputTag.type + "---" + inputTag.value);
}
}
</script>
</head>
<body>
<input type="text" value="Java">
<input type="text" value="Android">
<input type="text" value="Python">
<input type="text" value="Php">
<input type="password" value="abcdefg">
<button onclick="f()">确认</button>
</body>
</html>
通过name属性获取标签
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
var inputTag = document.getElementsByName("01");
for (var i = 0; i < inputTag.length; i++) {
var inputTag = inputTag[i];
console.log(inputTag.type + "---" + inputTag.value);
}
}
</script>
</head>
<body>
<input type="text" value="Java" name="01">
<input type="text" value="Android" name="02">
<input type="text" value="Python" name="03">
<input type="text" value="Php" name="04">
<input type="password" value="abcdefg" name="05">
<button onclick="f()">确认</button>
</body>
</html>
hasChildNodes()方法:根据有无子标签返回booleab值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
var formTag = document.getElementById("form1");
var r = formTag.hasChildNodes();
console.log(r);//true
}
</script>
</head>
<body>
<form action="" id="form1">
<input type="text" placeholder="请输入用户名">
<input type="password" placeholder="请输入密码">
</form>
<button onclick="f()">提交</button>
</body>
</html>
获取第一个子节点和最后一个子节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
var formTag = document.getElementById("form1");
//获取第一个子节点
var firstTag = formTag.firstChild;
alert(firstTag.type + "---" + firstTag.value);
//获取最后一个子节点
var lastTag = formTag.lastChild;
alert(lastTag.type + "---" + lastTag.value);
}
</script>
</head>
<body>
<form action="" id="form1"><input type="text" placeholder="请输入用户名"><input type="password" placeholder="请输入密码"><input
type="text" placeholder="请输入验证码"></form>
<button onclick="f()">提交</button>
</body>
</html>
替换节点
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//替换节点
function f() {
//根据标签获取节点
var aaTag = document.getElementById("aa");
var bbTag = document.getElementById("bb");
//获取父标签
var ulTag = aaTag.parentNode;
//调用replaceChild(替换, 被替换)
ulTag.replaceChild(bbTag, aaTag);
}
</script>
</head>
<body>
<ul>
<li id="aa">AA</li>
</ul>
<ul>
<li id="bb">BB</li>
</ul>
<button onclick="f()">点击</button>
</body>
</html>
获取和设置标签属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function f() {
//获取标签属性
//1.获取标签
var pTag = document.getElementById("01");
//2.获取标签属性
alert(pTag.getAttribute("name"));
alert(pTag.getAttribute("dog"))
}
</script>
</head>
<body>
<p dog="旺财" name="XA" id="01">小A</p>
<button onclick="f()">点击</button>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//给标签设置属性
window.onload = function f() {
//1.获取标签
var inputTag = document.getElementById("001");
//2.设置标签属性
inputTag.setAttribute("name", "bb");
alert(inputTag.getAttribute("name"));//name属性的值被设置为bb
}
</script>
</head>
<body>
<input type="text" name="aa" id="001">
<button onclick="f()">点击</button>
</body>
</html>