HTML事件屬性onfocus

2018-01-05 13:58 更新

HTML事件屬性onfocus


當(dāng)元素獲取焦點(diǎn)時,會觸發(fā) onfocus 屬性事件。

HTML5中的新功能

沒有。

句法

<element onfocus="script or Javascript function name">

支持的標(biāo)簽

所有HTML元素,EXCEPT:

<base>, 
<bdo>, 
<br>, 
<head>, 
<html>, 
<iframe>, 
<meta>, 
<param>, 
<script>, 
<style>, 
<title>


瀏覽器兼容性

onfocus Yes Yes Yes Yes Yes

例子

<!DOCTYPE html>
<html>
<body>

First name: <input type="text" id="fname" onfocus="myFunction(this.id)">

<script>
function myFunction(x) {
    console.log(x);
}
</script>

</body>
</html>

Click to view the demo



實(shí)施例2

下面的代碼顯示了如何添加事件偵聽器以輸入文本焦點(diǎn)事件。

<html>
<head>
<script language="JavaScript" type="text/javascript">
  function DisplayMsg(NumVal) {
    if (NumVal == 1) {
      alert("Type your name in the field");
    }
    if (NumVal == 2) {
      alert("Type your phone number in the field");
    }
  }
</script>
<title>Keyboard Event</title>
</head>
<body>
  <form name="form1">
    <b>Name:</b>&nbsp; <input type="text" name="text1"
      onFocus="DisplayMsg(1)" size="20">
    <P>
      <b>Phone:</b> &nbsp;<input type="text" name="text2"
        onFocus="DisplayMsg(2)" size="20">
    </p>
  </form>
</body>
</html>

Click to view the demo

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號