codecamp

HTML事件属性onblur

HTML事件属性onblur


触发 onblur 属性当元素失去焦点。

HTML5中的新功能

没有。

句法

<element onblur="script">

支持的标签

所有HTML元素,EXCEPT:

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


浏览器兼容性

onblur Yes Yes Yes Yes Yes

例子

<!DOCTYPE html>
<html>
<body>

Enter your first name: <input type="text" name="fname" id="fname" onblur="myFunction()">
Enter your last name: <input type="text" name="lname" id="lname" onblur="myFunction()">

<script>
function myFunction() {
    console.log("lost focus");
}
</script>

</body>
</html>

Click to view the demo



实施例2

下面的代码显示了如何听输入密码的Focus Lost事件。

<html>
<head>
<script type="text/javascript">
  function checkRequired() {
    document.write("value is required in field");
  }
</script>
</head>
<body>
  <form name="someForm">
    <input type="text" name="text1" /><br /> 
    <input type="password" name="text2" onblur="checkRequired();"/><br /> 
    <input type="hidden" name="text3" value="hidden value" />
    <textarea name="text4" cols=50 rows=10>The text area</textarea>
    <input type="submit" value="Submit" />
  </form>
</body>
</html>

Click to view the demo

HTML事件属性onbeforeunload
HTML事件属性onchange
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }