Loading

通过name也可以获取html元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form name="form1">
    <div>
        <label for="username">姓名</label>
        <input id="username" value="zyz">
    </div>
    <div>
        <label for="password">密码</label>
        <input id="password" value="123">
    </div>
</form>

<script>
    console.log(form1)  // 输出元素标签
    console.log(username)  // 输出元素标签
    console.log(password)  // 输出元素标签
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form name="form1">
    <div>
        <label for="username">姓名</label>
        <input id="username" value="zyz">
    </div>
    <div>
        <label >密码</label>
        <input name="password" value="123">
    </div>
    <button id="btn">按钮</button>
</form>

<script>
    console.log(document.form1.password.value)  // 123 反正name是有系统提示的
    console.log(document.form1.username.value)  // zyz id也能获取打印出来
</script>
</body>
</html>
posted @ 2024-11-23 15:29  一只大学生  阅读(2)  评论(0编辑  收藏  举报