定义和使用
focus() 方法用于为元素设置焦点(如果可以设置)。
提示:使用 blur() 方法来移除元素焦点。
浏览器支持
| 方法 | 谷歌 | IE | 火狐 | 苹果 | opera |
|---|---|---|---|---|---|
| focus() | Yes | Yes | Yes | Yes | Yes |
语法
HTMLElementObject.focus()参数
| None |
技术描述
| 返回值: | 没有返回值 |
|---|
实例
为 <a> 元素设置焦点:
<!DOCTYPE html>
<html>
<title>Web176教程网(web176.com)</title>
<head>
<style>
a:focus, a:active {
color: green;
}
</style>
</head>
<body>
<a id="myAnchor" href="https://www.web176.com" target="_blank">访问Web176教程网</a>
<p>点击按钮设置或移除以上链接的焦点。</p>
<input type="button" onclick="getfocus()" value="获取焦点">
<input type="button" onclick="losefocus()" value="移除焦点">
<script>
function getfocus() {
document.getElementById("myAnchor").focus();
}
function losefocus() {
document.getElementById("myAnchor").blur();
}
</script>
</body>
</html>
作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4380.html
支付宝
微信