关于点击按钮后事件响应的解决方案
问题描述:
有两个DIV (A和B),每个DIV都有按钮。
document.onkeydown = function(e){ var e = window.event?window.event:e; if(e.keyCode==13 || e.which == 13) { alert('tt'); return false; } }
<div><button type="button" id="a">点一下再按回国</button></div><div> <button type="button" id="b">点一下再按回国</button></div><script type="text/javascript"> $(document).ready(function(){ document.onkeydown = function(e){ var e = window.event?window.event:e; if(e.keyCode==13 || e.which == 13) { var id=$("button:focus").attr("id"); alert(id); } } })</script>
[解决办法]
var target = event.target || event.srcElement; switch(target.id) { case "btn1": case "btn2": }