jquery将自己的事件/函数添加到所选的html元素中?

时间:2022-01-10 22:05:00

is it possible to add to an element type or maybe a css selection an own event function?

是否可以添加元素类型或css选择自己的事件函数?

something like:

$("a").bind("blub", function() { alert("aaa" + this); });

$("a").get(0).blub();

i want define some functions which are only available for some special elements eg.:

我想定义一些仅适用于某些特殊元素的函数,例如:

the <div class="myDivContainer">...</div> should have the function available:

... 应该具有以下功能:

$("myDivContainer").get(0).blub();

but on maybe:

但也许:

$("myDivSeparator").get(0).blub(); 

this should not work, because it was not defined for it

这应该不起作用,因为它没有为它定义

is this possible?!

这可能吗?!

1 个解决方案

#1


You can create custom events with bind and trigger. See for example this question.

您可以使用bind和trigger创建自定义事件。例如,请参阅此问题。

You must use the trigger method to call the custom event:

您必须使用触发器方法来调用自定义事件:

$("a").bind("blub", function() {});
$("a").trigger("blub");

Of course you can trigger the "blub" event only for elements that bind has been called before.

当然,您只能为之前调用过绑定的元素触发“blub”事件。

If you want to use blub as a method, then you need to modify/extend the jQuery itself.

如果你想使用blub作为方法,那么你需要修改/扩展jQuery本身。

#1


You can create custom events with bind and trigger. See for example this question.

您可以使用bind和trigger创建自定义事件。例如,请参阅此问题。

You must use the trigger method to call the custom event:

您必须使用触发器方法来调用自定义事件:

$("a").bind("blub", function() {});
$("a").trigger("blub");

Of course you can trigger the "blub" event only for elements that bind has been called before.

当然,您只能为之前调用过绑定的元素触发“blub”事件。

If you want to use blub as a method, then you need to modify/extend the jQuery itself.

如果你想使用blub作为方法,那么你需要修改/扩展jQuery本身。