input的焦点事件

<body>
<h3>表单中文本框的focus和blur事件</h3>
<input id="txtest" type="text" value="" />
<div></div>

<script type="text/javascript">
(function(){
var html=document.querySelector('html')
html.style.fontSize=window.innerWidth/7.5+'px'
window.onresize=function(e){
html.style.fontSize=window.innerWidth/7.5+'px'
}
})()

//focus事件在元素获取焦点时触发,如点击文本框时,触发该事件;而blur事件则在元素丢失焦点时触发,如点击除文本框的任何元素,都会触发该事件
$(function(){
$('input').bind('focus',function(){
$('div').html('请输入你的姓名!')
})
$('input').bind('blur',function(){
if($(this).val().length==0){
$('div').html('你的名字不能为空!')
}
})
});
</script>
</body>

猜你喜欢

转载自www.cnblogs.com/yangwan/p/10395264.html