文本框自动获取焦点的两种方式

文本框自动获取焦点的两种方式

1、html5的autofocus属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
图书:<input type="text" name="book"><br/>
价格:<input type="text" name="price" autofocus>
</body>
</html>

2、通过js的focus()方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
图书:<input type="text" name="book"><br/>
价格:<input type="text" name="price" id="price">
<script>
    document.getElementById("price").focus();
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/Cheny_Yang/article/details/83615792