Jquery:停止传播? [英] Jquery: Stop propagation?

查看:69
本文介绍了Jquery:停止传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了stopPropagation,但是,我仍然连续获得两个弹出窗口。这比以前好一些,点击一个元素有20个弹出窗口....有更好的方法还是我错过了什么?

I have added stopPropagation, however, I still get two popups in a row. This is better than before, where there were 20 popups for one element that is clicked....is there a better approach or am I missing something ?

$(top.document).ready(function () {

    $("*").click(processAction);

});

function processAction(e) {
    var clicked = e.target;
    e.stopPropagation();
    alert(clicked.tagName);
    e.stopPropagation();
    switch (clicked) {
    case "A":
        //execute code block 1
        break;
    case "INPUT":
        //execute code block 2
        break;
    default:
        //code to be executed if n is different from case 1 and 2
    }
};


推荐答案

我肯定不会放置点击处理程序在每个元素上。正如@Rin所述,您可以按标签或其他选择器分配它们。

I'd say definitely do not place a click handler on every element. As @Rin stated, you can assign them by tag, or some other selector.

如果您确实想要处理页面上的所有点击这样,我建议您在文档上放置一个处理程序,然后让点击事件冒泡到那个。

If you really want to process all clicks on the page that way, I'd suggest that you place one handler on the document, and let the click events bubble up to that.

效率更高,无需 e.stopPropagation()

示例: http://jsfiddle.net/y6hry/

$(top.document).ready(function () {
       // All clicks on the page will bubble up to the document
       //   and fire the handler.
    $(document).click(processAction);
});

function processAction(e) {
    var clicked = e.target;
    alert(clicked.tagName);
    switch (clicked.tagName) {
    case "A":
        //execute code block 1
        break;
    case "INPUT":
        //execute code block 2
        break;
    default:
        //code to be executed if n is different from case 1 and 2
    }
};

这篇关于Jquery:停止传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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