onfocus 屬性用來獲取或設(shè)置當(dāng)前元素的focus事件的事件處理函數(shù)。當(dāng)前元素獲得鍵盤焦點(diǎn)時(shí)會(huì)觸發(fā)focus事件。
當(dāng)輸入域獲得焦點(diǎn)時(shí)運(yùn)行腳本:
<!DOCTYPE html> <html> <head> <title>HTML onfocus 事件屬性的使用(菜鳥教程 cainiaoplus.com)</title> <script> function setStyle(x) { document.getElementById(x).style.background="yellow"; } </script> </head> <body> <p>當(dāng)輸入域獲得焦點(diǎn)時(shí)setStyle函數(shù)被觸發(fā)。該函數(shù)改變輸入域的背景色。</p> 第一個(gè)名字: <input type="text" id="fname" onfocus="setStyle(this.id)"><br> 最后一個(gè)名字: <input type="text" id="lname" onfocus="setStyle(this.id)"> </body> </html>測(cè)試看看 ?/?
IEFirefoxOperaChromeSafari
所有主流瀏覽器都支持 onfocus 事件屬性
onfocus屬性在元素獲得焦點(diǎn)時(shí)觸發(fā)。
Onfocus通常與<input>,<select>和<a>一起使用。
提示:與 onfocus 屬性相反的屬性事件為onblur 。
注意: onfocus 屬性不能使用于以下元素: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, 或 <title>。
無。
<element onfocus="script">
值 | 描述 |
---|---|
script | 規(guī)定該onfocus事件觸發(fā)時(shí)執(zhí)行的腳本 |