HTML,Javascript中的自定义事件 [英] Custom event in HTML, Javascript

查看:246
本文介绍了HTML,Javascript中的自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML,JS中创建一个自定义事件。

I'm looking to create a custom event in HTML, JS.

<input type="text" oncustombind="foo(event);" />

如何创建一个? 'oncustombind'是我要创建的自定义事件。我应该可以调用oncustombind属性中定义的函数/代码。另外,我需要传递一些数据给事件对象。

How can I create one such? 'oncustombind' is the custom event I want to create. I should be able to call the function/code defined in the oncustombind attribute. Also, I need to pass some data to the event object.

我不想使用jQuery,YUI等任何库。

I don't want to use any libraries such as jquery, YUI.

任何帮助将深深感激。

推荐答案

你想要一个 CustomEvent

他们应该很容易使用但浏览器支持不佳。但是 DOM-shim 应该可以解决这个问题。

They should be easy to work with but browser support is poor. However the DOM-shim should fix that.

var ev = new CustomEvent("someString");
var el = document.getElementById("someElement");
el.addEventListener("someString", function (ev) {
    // should work
});
el.dispatchEvent(ev);

这篇关于HTML,Javascript中的自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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