按下滑鼠游標後失去輸入提示(獲取焦點)
假設我們的輸入文字如下 −
<label for="name">StudentName:<input name="name" id="studentName" value="Enter your Name ????" class="guess"></label>
要清除按滑鼠游標產生的輸入提示,請使用 onFocus 和 onBlur 概念。
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<label for="name">StudentName:<input name="name" id="studentName"
value="Enter your Name ????" class="guess"></label>
</div>
<script>
function guessFocus() {
if (this.value == this.defaultValue)
this.value = '';
}
function guessBlur(event) {
if (this.value == '')
this.value = this.defaultValue;
}
var event = document.getElementById('studentName');
event.onfocus = guessFocus;
event.onblur = guessBlur;
</script>
</body>
</html>要執行以上程式,請儲存檔名“anyName.html(index.html)”並右鍵單擊該檔案。在 VS Code 編輯器中選擇“使用 Live Server 開啟”選項。
輸出
這將產生以下輸出 −

當您單擊文字中的滑鼠時,您將獲得焦點並清除提示。螢幕截圖如下 −

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP