如何模仿 stopImmediatePropagation() 的行为(不使用 jquery)

2023-04-19前端开发问题
2

本文介绍了如何模仿 stopImmediatePropagation() 的行为(不使用 jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在为一个 Javascript 库编写事件处理代码,并且我正在尝试实现类似于 stopImmediatePropagation() 的东西,它也适用于 IE 6.

I'm working on the event-handling code for a Javascript library, and I'm trying to implement something similar to stopImmediatePropagation() that will also work in IE 6.

目前事件处理的工作方式是,我们的事件处理代码向对象注册,然后用户向我们的事件处理程序注册他们所有的事件.

The way the event handing works currently, is that our event handing code registers with the object, and then users register all their events with our event handlers.

我尝试模拟 stopImmediatePropagation() 的第一种方法是简单地将该方法添加到事件中(如果它尚不存在):

The first way I tried to emulate stopImmediatePropagation() was to simply add that method to the event if it didn't already exist:

if (event != null && event.isImmediatePropagationEnabled == null) {
    event.stopImmediatePropagation = function () {
        this.isImmediatePropagationEnabled = false;
    };
    event.isImmediatePropagationEnabled = true;
    event.isImmediatePropagationStopped = function () {
        return !this.isImmediatePropagationEnabled;
    };
}

当用户的事件处理回调被调用时,它们被传递到我们得到的同一个事件对象中.如果他们愿意,他们可以在事件上调用 stopImmediatePropagation().

When the users' event handler callbacks are called, they're passed in the same event object we get. They can call stopImmediatePropagation() on the event if they wish.

当我们遍历所有向我们注册的事件处理回调时,我们每次都检查传播布尔值:

As we're looping through all the event handling callbacks registered with us, we check the propagation boolean each time:

given some event
for [all the callbacks] {
    if (event != null &&
        event.isImmediatePropagationStopped != null &&
        event.isImmediatePropagationStopped()) {
        stopPropagation = true;
        break;
    }

    execute the callback, passing in the event
}

这在某些浏览器中效果很好.因为事件持续存在,即使我们的事件处理代码退出并且事件冒泡到下一个元素,一旦我们的事件处理代码再次被命中,isImmediatePropagationStopped 属性仍然存在,因此不再执行回调(向我们注册的).

This works great in some browsers. Because the event persists, even once our event handling code exits and the event bubbles up to the next element, once our event handing code gets hit again the isImmediatePropagationStopped property still exists, and so no more callbacks (that are registered with us) are executed.

在 Internet Explorer(甚至 8)中,这不起作用.在同一个元素上,一切都很好;但是一旦事件冒泡,似乎生成了一个全新的事件对象,并且我们失去了 isImmediatePropagationStopped 属性.这意味着我们无法检查用户是否关闭了传播.

In Internet Explorer (even in 8), this doesn't work. On the same element, things are fine; but once the event bubbles, it seems like an entirely new event object is generated, and we lose the isImmediatePropagationStopped property. This means we can't check if the user turned off propagation.

所以我的问题是,有没有人对如何做到这一点有任何想法?我知道 jquery 管理类似的壮举( http://bugs.jquery.com/ticket/3355 ),但它们以类似的方式进行 - 将其存储在对象的额外数据中.我不知道的是,物体的不持久性如何不会像我一样伤害他们.(而且由于各种原因,这里不能使用 jquery 本身)

So my question is, does anyone have any ideas on how to do this? I know that jquery manages a similar feat ( http://bugs.jquery.com/ticket/3355 ), but they do it a similar way - storing it in extra data for the object. What I don't know is how the non-persistence of the object doesn't hurt them like it does me. (And for various reasons, using jquery itself is not an option here)

如果有人有任何见解——要么是因为比我更了解 Javascript,要么是因为他们能想到一种巧妙的方法来做到这一点——我将不胜感激.谢谢!

If anyone has any insights - either due to knowing more about Javascript than me, or because they can think of a neat way to do this - I'd greatly appreciate it. Thanks!

推荐答案

试着改变你的函数:

event.stopImmediatePropagation = function () {
    this.isImmediatePropagationEnabled = false;
    this.cancelBubble = true;
};

cancelBubble"标志是 IE 的东西,它应该防止事件冒泡父 DOM 元素链.

That "cancelBubble" flag is an IE thing, and it should prevent the event from bubbling up the parent DOM element chain.

这篇关于如何模仿 stopImmediatePropagation() 的行为(不使用 jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Fatal error: Call to a member function fetch_assoc() on a no
业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass...
2024-12-13 前端开发问题
136

layui实现laydate日历控件控制之前日期不可选择
具体实现代码如下: laydate.render({ elem: '#start_time', min:0, //,type: 'date' //默认,可不填}); 只要加一个min参数,就可以控制了。0表示之前的日期不可...
2024-11-29 前端开发问题
133

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313