古詩詞大全網 - 成語經典 - JS中如何判斷壹元素是否獲得焦點?

JS中如何判斷壹元素是否獲得焦點?

需要準備的材料分別是:電腦、html編輯器、瀏覽器。

1、首先,打開html編輯器,新建html文件,例如:index.html。

2、在index.html的<script>標簽中,輸入js代碼:

$('body').append(document.activeElement.id == "a" ? 'true': 'false');

$('#a').focus(function () {

$('body').append(document.activeElement.id == "a" ? 'true': 'false');

});

3、瀏覽器運行index.html頁面,此時會發現false代表沒有聚焦,點擊聚焦後發現打印了true。