jBox 是个不错的对话框组件。 在 ASP.NET Form 中使用 jBox 的时候,在按钮注册的客户端点击事件中,会发现不能弹出对话框问题。 表现为页面一闪就提交了,导致对话框一闪而过,甚至根本看不到。导致模式对话框失败。 首先,按钮会有默认处理,对于普通的 ASP.NET 按钮来说,会导致表单的提交,提交表单导致了页面的刷新。所以,为了不提交表单,就需要阻止按钮默认的行为,这可以通过下面的代码实现。 复制代码 代码如下: function stopDefault( e ) { // Prevent the default browser action (W3C) if ( e && e.preventDefault ) e.preventDefault(); else // A shortcut for stoping the browser action in IE window.event.returnValue = false; return false; }
// 设置在关闭对话的时候提交表单 var options = { closed: function () { alert("submit"); // 找到需要提交的表单 $("#" + form1Id ).submit(); } };
// 显示 jBox 对话框 var info = "jQuery jBox<br /><br />版本:v2.0<br />日期:2011-7-24<br />"; info += "官网:<a target="_blank" href="http://kudystudio.com/jbox">http://kudystudio.com/jbox</a>"; $.jBox(info, options );
// 阻止默认的事件处理 stopDefault(e);
});
对于 jQuery 来说,在事件处理方法中返回 false 可以完成类似功能。
但是这两种方式是有区别的。return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。 stopDefault 则只阻止默认事件本身,不阻止事件冒泡。 还可以阻止事件冒泡,这需要调用下面的方法。 复制代码 代码如下: function stopBubble(e) { // If an event object is provided, then this is a non-IE browser if (e && e.stopPropagation) // and therefore it supports the W3C stopPropagation() method e.stopPropagation(); else // Otherwise, we need to use the Internet Explorer // way of cancelling event bubbling window.event.cancelBubble = true; }