首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

怎么通过JS 给一个ID添加触发事件

2012-08-13 
如何通过JS 给一个ID添加触发事件?!DOCTYPE htmlhtmlhead/head? script? window.onloadfc?? f

如何通过JS 给一个ID添加触发事件?

<!DOCTYPE html>
<html>
<head>

</head>
? <script>
? window.onload=fc;
?? function fc(){
?? var dss=document.getElementById("inputid2");
??? dss.onblur=function(){
?? ? alert(dss.id);
?? ? }
?? ?}
? </script>
<body>
<input type="text" id="inputid2">
</body>
</html>

?

?

?

?

网上别人总结的。

?

?

?

下面是jQuery的写法

?

?

<!DOCTYPE html>
<html>
<head>
? <style>p { color:red; }</style>
? <script src="http://code.jquery.com/jquery-latest.min.js"></script>
? <script>
? $(document).ready(function () {
? $("input").blur(function(){
??? alert(this.id);
? });
});
? </script>
</head>
<body>
? <input type="text" id="inputid">
</body>
</html>

热点排行