Trigger a click event on an inner element

前端 未结 14 628
后悔当初
后悔当初 2020-12-29 07:53

A row in a table where each first cell contains a link needs to be clicked and open a url.

14条回答
  •  再見小時候
    2020-12-29 08:27

    I think .click() or .trigger("click") only fires the event handlers for onclick.

    See a sample here http://jsfiddle.net/sethi/bEDPp/4/ . Manually clicking on the link shows 2 alerts while firing the event through jQuery shows only 1 alert.

    You can also refer to this link : re-firing a click event on a link with jQuery

    Solution

    If you are just looking to open a fancybox try this:

    $("table tr").bind('click',function(e) {
            var elem = $(e.target);
            if(elem.is('a')){
                return;    
            }
            e.stopImmediatePropagation();
            var parent= elem.is('tr') ? elem:elem.parents("tr").eq(0);
            parent.find("a").trigger('click.fb');
        });
    

    where click.fb is the event that fancybox binds with the anchor element.

提交回复
热议问题