今天凑巧碰到一件怪事:
1.问题描述:
根据用户输入的内容按回车键作查询时,在IE下一直会触发一个Button按钮;IE下只有Button才会有这个问题,如果是a标签就不会有这样的问题?
2.解决思路:
比如可以把button换掉;阻止keydown事件的默认行为preventDefault等。
3.举例说明:
1.阻止浏览器的默认行为
function stopDefault(e) {
if(e && e.preventDefault) { //如果提供了事件对象,则这是一个非IE浏览器
e.preventDefault(); //阻止默认浏览器动作(W3C)
} else { //IE中阻止函数器默认动作的方式
window.event.returnValue = false;
}
return false;
}
2.停止事件冒泡
function stopBubble(e) {
if(e && e.stopPropagation) { //如果提供了事件对象,则这是一个非IE浏览器
e.stopPropagation(); //因此它支持W3C的stopPropagation()方法
} else { //否则,我们需要使用IE的方式来取消事件冒泡
window.event.cancelBubble = true;
}
return false;
}
<input type="text" name="appGrpName_s" id="appGrpName_s" onkeydown="enter_down(this.form, event);"/>
<script type="text/javascript">
function enter_down(form, event) {
if(event.keyCode== "13") {
stopDefault(event);
submitForm(form,'actionDiv');
}
}
function stopDefault(e) {
if(e && e.preventDefault) { //如果提供了事件对象,则这是一个非IE浏览器
e.preventDefault(); //阻止默认浏览器动作(W3C)
} else { //IE中阻止函数器默认动作的方式
window.event.returnValue = false;
}
return false;
}
</script>
这样就可以解决ie下面按回车键触发button click()事件了