JS中onblur总结
#第一种方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="text"id="user">
<script>
document.getElementById("user").onblur=function (anyparameter) {
if(this.value==""){
alert("输入为空!");
}
}
</script>
</body>
</html>
**element.οnblur=function(){} ------**右边的函数必须为匿名函数,右边也可以为函数对象,如下第二种方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="text"id="user">
<script>
document.getElementById("user").onblur=fun;
function fun(anyparameter) {
if(this.value==""){
alert("输入为空!");
}
}
</script>
</body>
</html>
第三种,在HTML标签中调用,js中定义函数:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="text"id="user" onblur="fun(this)">
<script>
function fun(anyparameter) {
if(anyparameter.value==""){
alert("输入为空!");
}
}
</script>
</body>
</html>
注意:上述函数传递的参数, this指针指向指针当前所在标签或函数, ‘=‘在js事件中表示的含义如同java中的’ .’ , 一二种方法自动将指针传递给函数,所以参数是任意的,反正用不到, 可以缺省 … 第三种必须将指针作为参数传递给函数, 且js脚本中定义函数时不能将this关键字作为接收参数