
html中的表单:
<!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>Document</title>
</head>
<body>
<form action="" name="hello" id="login">
<input type="text" placeholder="demo@email.com"
<button>提交</button>
</form>
</body>
</html>利用document.forms获取当前页面的所有表单
document.forms[0]获取当前页面的第一个表单
document.forms['hello']通过表单的name属性获取表单
document.forms['login']通过表单的id属性获取表单
立即学习“Java免费学习笔记(深入)”;
<script>
//form
console.log(document.forms);
// <form action="" name="hello" id="login">
console.log(document.forms[0]);
console.log(document.forms['hello']);
console.log(document.forms['login']);
console.log(document.forms.item(0));
console.log(document.forms.item("hello"));
console.log(document.forms.item("login"));
//推荐 id
console.log(document.forms.namedItem("login"));
console.log(document.forms.hello);
console.log(document.forms.login);
</script>推荐:《2021年js面试题及答案(大汇总)》
以上就是如何在JavaScript中获取表单元素的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号