Trigger event in jquery without a namespace

只愿长相守 提交于 2019-12-10 03:26:23

问题


With the current version of jQuery (1.8.3) you can still use the following code (showing both a click with and without a namespace):

$("span")
    .on("click", function(e) { console.log('click without a namespace'); })
    .on("click.namespace", function(e) { console.log('click with a namespace'); })
    .trigger("click!"); // "click without a namespace"

JSFiddle

However, that seems to be removed in 1.9. Is there a way in the new version of jQuery (the test version) to replicate this feature? Or do I have to create a custom event like this:

$("span")
    .on("click", function(e) { $(this).trigger("customevent"); })
    .on("click.namespace", function(e) { console.log('click with a namespace'); })
    .on("customevent", function(e) { console.log('"click" without a namespace'); })
    .trigger("customevent");

JSFiddle


回答1:


You can call that using

.trigger("click.$")

as we found it is because regex

http://jsfiddle.net/oceog/RczDZ/10/



来源:https://stackoverflow.com/questions/13691402/trigger-event-in-jquery-without-a-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!