English 中文(简体)
我可以使用 jQuery 将多个点击事件附加到元素上吗?
原标题:Can I attach multiple click events to an element using jQuery?
  • 时间:2012-05-23 16:22:25
  •  标签:
  • jquery

我在插件脚本中有以下内容:

$("#menu")
.on( click , a[href^="/Test"] ,function(event){

        event.preventDefault();
        var href = $(this).attr( href );

        alert(this);
        // Load content
        $( #content ).load(href,   , function (responseText, textStatus, XMLHttpRequest) {
            $(this).applyTemplateSetup().buildTableOfContent();
        });
        window.location.hash =  #  + href;

        // Get nav link
        var a = $( #menu a[href="  + href +  "] );
        if (a.length > 0) {
            // Mark as current
            $( #menu a ).removeClass( current );
            a.addClass( current );

            // Update breadcrumb
            var breadcrumb = $( #breadcrumb ).empty();
            while (a.length > 0) {
                if (a.get(0).nodeName.toUpperCase() ==  A ) {
                    var target = a;
                }
                else {
                    var target = a.parent().find( a:first );
                }
                breadcrumb.prepend( <li><a href="  + target.attr( href ) +  ">  +
                a.contents().filter(function () {
                    return this.nodeType == 3;
                }).first().text()
                +  </a></li> );

                // Check if opened
                var li = a.parent();
                if (li.hasClass( closed )) {
                    li.removeClass( closed );
                }

                a = li.parent().parent().children( a, span );
            }
        }
    });       

I would like to also set the value of a link when the user clicks. I added this on my page but it seems like it does not get called. Is there a problem when I attach two of the same type of events to an element?

$("#menu")
            .on( click ,  a[href^="/Test"] , function (event) {
                event.preventDefault();
                var href = $(this).attr( href );
                var pk = href.substr(7, 4);
                var rk = href.substr(12, 4);
                var link = "/Admin/Testss/Edit?pk=" + pk + "&rk=" + rk;
                $( #editLink ).data( href , link);
            });
最佳回答

是的,它应该有效。确保定义第二个处理器的代码正在实际执行中 。

或者,您可以随时修改您所附的第一个调用您(多个)其他函数的处理器。

我个人更喜欢让前一名工作 因为它允许你 随身携带/分解处理器

如果您使用 < a href=> "http://api.jquery.com/bind/" rel="nofollow" > Bind , 您是否有同样的问题?

如关于Q的评论所述,如果任何函数返回了 false ,则会防止事件爆发。

问题回答

只要保持它在一个点击事件中, 将它分为两个是多余的





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签