<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>函数接收参数并弹出</title>
<style>
body{
display: flex;
}
.wrap{
margin: auto;
}
form input{
display: block;
margin: 5px 0;
}
#submit{
margin: auto;
}
</style>
</head>
<body>
<form class="wrap">
<input type="text" value="广州市" />
<input type="text" value="番禺区" />
<input id="submit" type="button" value="传参" />
</form>
<script>
window.onload = function () {
var oInput = document.getElementsByTagName("input");
console.log(oInput);
var btn = document.getElementById("submit");
btn.onclick = function () {
for (var i in oInput) if (oInput[i].type === "text") {
alert(oInput[i].value);
}
}
}
</script>
</body>
</html>
1.如果存在许多input标签,就不能给每个标签加上id,如果只是想要弹出信息,则直接遍历并判断type来输出。