gpt4 book ai didi

javascript - 你能用纯 JavaScript 绑定(bind)到 jQuery 事件吗?

转载 作者:行者123 更新时间:2023-11-30 13:27:16 25 4
gpt4 key购买 nike

我正在从事一个项目,许多不同的公司都在同一个网站上工作。

主要开发人员已经设置了一个事件 - 让我们称之为 init - 这表明页面已准备好执行我们的代码。

他们基本上是这样调用它的:

$(window).trigger('init');

出于多种原因,我不会在这里详述,我们宁愿尽可能避免在我们自己的代码中使用 jQuery。我试着像这样绑定(bind)它:

window.addEventListener('init', function (event) {
alert('hehehehe');
});

但这似乎行不通。不过,这非常有效:

$(window).bind('init', function (event) {
alert('hehehehe');
});

jQuery 是否默认使用您无法使用普通 JS 绑定(bind)的特殊事件对象?我只是在做傻事吗?

最佳答案

docs for bind似乎包含答案:

Any string is legal for eventType; if the string is not the name of a native DOM event, then the handler is bound to a custom event. These events are never called by the browser, but may be triggered manually from other JavaScript code using .trigger() or .triggerHandler().

没有名为 'init' 的原生 DOM 事件:

http://en.wikipedia.org/wiki/DOM_events

因此“浏览器永远不会调用这些事件,但可以使用 .trigger() 或 .triggerHandler() 从其他 JavaScript 代码手动触发”

关于javascript - 你能用纯 JavaScript 绑定(bind)到 jQuery 事件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8045146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com