onblur:只要input失去焦点就会触发 onblur事件。不管input框里面的值是否改变,都会触发事件。onchange:只有当input框里面的值发生变化才会执行,这里加了值判断 。
请看下面实例:
<code><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<script type="text/javascript">
function init(){
function testChange(){
this.value=this.value.toUpperCase();
document.getElementById("onchangeDiv").innerHTML=Number(document.getElementById("onchangeDiv").innerHTML)+1;
}
function testblur(){
this.value=this.value.toUpperCase();
document.getElementById("onblurDiv").innerHTML=Number(document.getElementById("onblurDiv").innerHTML)+1;
}
var input1=document.getElementById("input1");
var input2=document.getElementById("input2");
input1.onchange=testChange;
input2.onblur=testblur;
}
window.onload=init;
</script>
</head>
<body>
<p>触发onchange <span id="onchangediv">0</span>次</p>
<p>触发onblur <span id="onblurDiv">0</span>次</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2104">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680121868279.png" alt="稿定AI社区">
</a>
<div class="aritcle_card_info">
<a href="/ai/2104">稿定AI社区</a>
<p>在线AI创意灵感社区</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="稿定AI社区">
<span>60</span>
</div>
</div>
<a href="/ai/2104" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="稿定AI社区">
</a>
</div>
<input type="text" id="input1" value="test change"/><br/>
<input type="text" id="input2" value="test blur"/>
</body>
</html></code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号