js阻止默认浏览器行为与冒泡行为的实现代码(2)

但是,有时候还是不能解决相应的问题,明明已经调用了阻止浏览器默认行为的方法,可在按回车的时候还是会调用默认行为,最终也没有找到问题所在,只好把回车键禁用了,实际上是用Tab键代替回车键。代码如下:

<script language="javascript" event="onkeydown" for="document"> //若为回车键 if ( event.keyCode == 13 ) { //改成Tab键,这样每次按回车都起到了Tab的功效,光标跳到下一个对象 event.keyCode = 9; } </script> <script language="javascript" type="text/javascript"> //禁用Enter键表单自动提交 document.onkeydown = function(event) { var target, code, tag; if (!event) { event = window.event; //针对ie浏览器 target = event.srcElement; code = event.keyCode; if (code == 13) { tag = target.tagName; if (tag == "TEXTAREA") { return true; } else { return false; } } } else { target = event.target; //针对遵循w3c标准的浏览器,如Firefox code = event.keyCode; if (code == 13) { tag = target.tagName; if (tag == "INPUT") { return false; } else { return true; } } } }; </script>

具体用法试例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/pages/common/global.jsp"%> <html> <head> <title>脚本之家</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <script> function gotoPage(currentPage,form) { goto_Page(currentPage,form,"actionDiv"); } function addAppGrp(){ $("#actionDiv").load("${contextPath }/pages/manage/business/add.jsp"); $("#chance_search_div").hide(); } function modifyAppGrp(idName){ var id=encodeURIComponent(idName); var url = contextName + "/appGrpAction.do?method=addAppGrp&appGrpName="+id; retrieveURL(url,'actionDiv'); $("#chance_search_div").hide(); } function savebusiness(thisForm){ var name = $("#appGrpName").val(); if(name.trim()==""){ alert("分组名称不能为空。"); return; } submitForm(thisForm,null,afterSave); return ; } function afterSave(content){ if(content!=null&&content!=""){ var arr = content.split(","); if(arr[0]=="true"){ $("#chance_search_div").show(); //当前结点 var itemId = "0::" + $("#appGrpName").val(); //父结点,因为只有添加根应用分组时才会执行这个方法,所以父结点统一为-1 var parentId = -1; //当前结点显示名称 var itemText = $("#appGrpName").val(); //添加新结点 tree.insertNewChild(parentId, itemId, itemText, doOnClick, 'myfolderClosed.gif' ,'myfolderOpen.gif','myfolderClosed.gif'); retrieveURL("${contextPath}/appGrpAction.do?method=appGrpList","actionDiv"); return; } alert(arr[1]); return; } alert("保存失败"); return; } function deleteBusiness(thisForm,idName){ if(confirm("确认要删除么?")){ var id=encodeURIComponent(idName); retrieveURL("${contextPath}/appGrpAction.do?method=deleteAppGrp&appGrpName=" + id,null,null,function(content){ if(content!=null&&content!=""){ var arr = content.split(","); if(arr.length==3&&arr[2]=='y'){ var msg = "该应用组下有应用,要强制删除么?"; if(confirm(msg)){ retrieveURL("${contextPath}/appGrpAction.do?method=forceDelAppGrp&appGrpName="+id,null,null,afterForceDel); } return; } if(arr.length==2){ if(arr[0]=="true"){ //当前结点 itemId = "0::" + idName; tree.deleteItem(itemId); retrieveURL("${contextPath}/appGrpAction.do?method=appGrpList","actionDiv"); return; } alert(arr[1]); } return; } alert("删除失败"); return; }); return ; } } function afterForceDel(){ if(content!=null&&content!=""){ var arr = content.split(","); if(arr[0]=="true"){ monitorTree(); retrieveURL("${contextPath}/appGrpAction.do?method=appGrpList","actionDiv"); return; } alert(arr[1]); return; } alert("保存失败"); return; } function enter_down(form, event) { if(event.keyCode== "13") { stopDefault(event); submitForm(form,'actionDiv'); } } function stopDefault(e) { //如果提供了事件对象,则这是一个非IE浏览器 if(e && e.preventDefault) {   //阻止默认浏览器动作(W3C)   e.preventDefault(); } else {   //IE中阻止函数器默认动作的方式   window.event.returnValue = false; } return false; } </script> </head> <body> <table> <tr> <td> <div> <html:form action="appGrpAction.do?method=appGrpList"> <table> <tr> <th> 查询 <input type="hidden" value="1" /> </th> </tr> <tr> <td> <br /> 名称 <input type="text" onblur="javascript:isSpecialChar(this);" onkeydown="enter_down(this.form, event);"/> <input type="button" value="查 询" /> <input type="button" value="添 加"/> <br /> </td> </tr> </table> </html:form> </div> <div></div> </td> </tr> </table> <script><!-- $("#actionDiv").load("${contextPath }/appGrpAction.do?method=appGrpList&random=" + Math.random()); --></script> </body> </html>

以上这篇js阻止默认浏览器行为与冒泡行为的实现代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzpfdz.html