是HTML标签上的特性
inputDom.getAttribute("id")
inputDom.setAttribute("id", "inputId2")
<input id="inputId" type="text" value="name" />
var inputDom = document.querySelector("#inputId");
console.log(inputDom.getAttribute("id"));// "inputId"
inputDom.setAttribute("id", "inputId2");
console.log(inputDom.getAttribute("id")); // "inputId2"
console.log(inputDom.getAttribute("type")); // text
inputDom.setAttribute("type", "007");
console.log(inputDom.getAttribute("type"));// 007
console.log(inputDom.getAttribute("value"));// name
inputDom.setAttribute("value", "007");
console.log(inputDom.getAttribute("value"));// 007
inputDom.value = "008";
console.log(inputDom.getAttribute("value"));// 007
是DOM中的属性,是JavaScript里的对象;
inputDom.id
inputDom.value
inputDom.value = "008"
<input id="inputId" type="text" value="name" />
var inputDom = document.querySelector("#inputId");
console.log(inputDom.id);// "inputId"
inputDom.setAttribute("id", "inputId2");
console.log(inputDom.id); // "inputId2"
console.log(inputDom.type)); // text
inputDom.setAttribute("type", "007");
console.log(inputDom.type));// text
console.log(inputDom.value));// name
inputDom.setAttribute("value", "007");
console.log(inputDom.value));// name
inputDom.value = "008";
console.log(inputDom.getAttribute("value"));// 008
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Documenttitle>
head>
<body>
<input id="inputId" type="text" value="name" />
body>
<script>
var inputDom = document.querySelector("#inputId");
console.log(inputDom.getAttribute("id"));
console.log(inputDom.id);
// "inputId"
// "inputId"
inputDom.setAttribute("id", "inputId2");
console.log(inputDom.getAttribute("id"));
console.log(inputDom.id);
// "inputId2"
// "inputId2"
inputDom.id = "inputId";
console.log(inputDom.getAttribute("id"));
console.log(inputDom.id);
// "inputId"
// "inputId"
console.log(inputDom.getAttribute("type"));
console.log(inputDom.type);
// text
// text
inputDom.setAttribute("type", "007");
console.log(inputDom.getAttribute("type"));
console.log(inputDom.type);
// 007
// text
inputDom.type = "008";
console.log(inputDom.getAttribute("type"));
console.log(inputDom.type);
// 008
// text
console.log(inputDom.getAttribute("value"));
console.log(inputDom.value);
// name
// name
inputDom.setAttribute("value", "007");
console.log(inputDom.getAttribute("value"));
console.log(inputDom.value);
// 007
// 007
inputDom.value = "008";
console.log(inputDom.getAttribute("value"));
console.log(inputDom.value);
// 007
// 008
script>
html>