input输入框里怎么做判断

2025-11-21 05:04:46

1、创建一个 html 的文档 hello.html

2、<!doctype html>

<html>

<head>

<meta charset="utf-8" />

<title>hello</title>

</head>

<body>

age : <input name="age" type="text" id="age" />

<input type="button" value="获取input的值" id="btnGetvalue">

<script>

window.onload = function(){

function $(id){                                      // 定义一个 根据 ID 获取元素的方法

var node = document.getElementById(id) ;

if(!window.attachEvent){                         // 兼容不同浏览器的的事件添加

node.attachEvent = function(eName, e){

node.addEventListener(eName, e) ;

}

}

return node ;

}

$("btnGetvalue").attachEvent("click", function(e){   // 添加一个 单击事件

alert(e) ;

console.log(e) ;

alert(ageNode.value);

}); 

$("age").attachEvent("blur", function(e){           // 添加一个焦点事件

console.log(e);

this.style.color = "red" ;

});

$("age").attachEvent("mouseenter", function(e){       // 添加一个鼠标进入事件

console.log(e);

this.style.backgroundColor = "green" ;

});

$("age").attachEvent("mouseout", function(e){       // 添加一个鼠标移出事件

console.log(e);

this.style.backgroundColor = "white" ;

});

}

</script>

</body>

</html>

3、定义一个 根据 ID 获取元素的方法 (兼容不同浏览器的的事件添加)

添加一个 单击事件

添加一个焦点事件

添加一个鼠标进入事件

添加一个鼠标移出事件

4、用浏览器打开,看一下效果。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢