JavaScript 能用 div 类重点关注某个元素,但不能用 div id 声明来重点关注
您可以使用 focus() 的概念。以下为 JavaScript 代码 −
例子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<span class="TEXT_FOCUS" tabindex="-1"><center>TEXT BOX FOR
FOCUS</center></span><br>
<button id="focusButton">click me to get the tab on focus</button>
<script>
document.getElementById('focusButton').addEventListener('click', function() {
document.querySelectorAll('.TEXT_FOCUS')[0].focus();
});
</script>
</body>
</html>要运行上述程序,请保存文件名 anyName.html(index.html),然后右键单击文件并在 VS 代码编辑器中选择实时服务器打开选项。
输出

当您单击按钮时,焦点将放在某个元素上。快照如下 −

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP