知道事件的前一个处理程序是否返回false。 defaultPrevented in IE< 9? [英] Knowing if a previous handler for an event returned false. defaultPrevented in IE < 9?

查看:99
本文介绍了知道事件的前一个处理程序是否返回false。 defaultPrevented in IE< 9?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在应用程序中放置了全局模式正在加载,请稍候< div> 每次点击< a> < input type =button> 时,它会显示。这是通过在 onload中分配< body> 一个 onclick 事件处理程序来实现的, code>,以便在事件发生时执行:

 < body onload =setup() > 
<! - 无法控制这里的内容 - >
< / body>

< script>
function setup(){
document.getElementsByTagName('body')[0] .onclick = clickFunc;
}
函数clickFunc(eventData){
var clickedElement =(window.event)? event.srcElement:eventData.target;
if(someConditions){
document.getElementById('modal')。style.display =block;
}
}
< / script>

现在,我在标记中有一些组件(我无法控制) 在他们的 onclick 中返回false 。这导致模态弹出窗口显示,只要有一个 confirm(),并且用户拒绝。可能正确的处理方式是 preventDefault(),以便事件不会起泡,如 event.preventDefault()vs. return false



但是我没有控制权,所以:有没有办法(最好是非jquery +跨浏览器)知道以前执行的结果事件处理程序(在较低级别)?



如果没有,我想我只需要不要显示模态< div> 每当组件的 onclick 包含返回false



是一个 JSFiddle 进行测试。



更新



下面的Bergi的回答是我正在搜索的内容。但是,在IE< code> defaultPrevented 似乎不被支持9.有没有一个等效的/解决方法来实现在IE<你可以检查(window.event?event.defaultPrevented:eventData.isDefaultPrevented( ))== false 捕获已传回的的事件 false


I've put a global modal "Loading, please wait" <div> in an application. It is showing every time an <a> or <input type="button"> is clicked. This is achieved by assigning the <body> an onclick event handler in onload, so that it gets executed when the event bubbles up:

<body onload="setup()">
    <!-- No control over what will be here -->
</body>

<script>
function setup() {
    document.getElementsByTagName('body')[0].onclick = clickFunc;
}
function clickFunc(eventData) {
    var clickedElement = (window.event) ? event.srcElement : eventData.target;
    if (someConditions) { 
        document.getElementById('modal').style.display = "block";
    }
}
</script>

Now, I've got some components in the markup (over which I have no control) that do return false in their onclicks. That's causing the modal popup to show and just stay there when there's a confirm() and the user denies. Probably the proper way of dealing with this would have been preventDefault(), so that the event wouldn't bubble up, as explained in event.preventDefault() vs. return false.

But I don't have control over that part, so: Is there a way (preferrably non-jquery+cross-browser) to know the result of previous executions of the event handler (at lower levels) ?

If there isn't I guess I'll just have to refrain from showing the modal <div> whenever the component's onclick contains return false.

Here is a JSFiddle for testing.

UPDATE

Bergi's answer below is what I was searching for. However, defaultPrevented doesn't seem to be supported in IE < 9. Is there an equivalent/workaround to achieve that in IE < 9?

解决方案

You could check for (window.event ? event.defaultPrevented : eventData.isDefaultPrevented()) == false to catch propagated events that have not returned false.

这篇关于知道事件的前一个处理程序是否返回false。 defaultPrevented in IE&lt; 9?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆