
<html>
<body>
<div>
<input type="text" name="time" id="time" />
<input type="button" value="开始" Onclick="startime()" id="button"/>
</div>
<script type="text/Javascript">
function startime(){
console.debug(this);
}
</script>
</body>
</html>
如何才能获取
<input type="button" value="开始" Onclick="startime()" id="button"/>
这个事件源?
1 Septembers 2015-06-08 01:06:22 +08:00 HTML: <input type="button" value="开始" Onclick="startTime(this)" id="button"/> Javascript: function startTime(element) { console.log(element); } |
2 w88975 2015-06-08 01:08:05 +08:00 Onclick="startTime(event)" function startTime(event) 一个event搞定一切 |
3 yyfearth 2015-06-08 03:38:27 +08:00 现在了 压根不该这样写 绑定事件应该用js来绑定 而不是写在html里面 至少用 document.getElementById('button').Onclick= function startime(e){ console.debug(this, e); } 一般来说最好用library比如jQuery或者类似 $('#button').click(function startime(e){ console.debug(this, e); }); 或者原生的 document.getElementById('button').addEventListener('click', function startime(e){ console.debug(this, e); }, false); |
4 flowfire 2015-06-08 09:24:49 +08:00 要么使用监听函数。 要么用call Onclick= startTime.call(this, a,b,c) function starttime(a,b,c){} |
5 sujin190 2015-06-08 09:36:36 +08:00 其实你在chrome控制台调试下,看下调用栈和各作用域变量就知道了 |
7 coolzjy 2015-06-08 10:12:46 +08:00 难道不是 event.target? |