trigger a click on a anchor link

社会主义新天地 提交于 2019-12-02 03:17:51
Tats_innit

Hiya Working demo here: http://jsfiddle.net/f6FJ3/19/

for the trigger reason read this : jQuery: trigger click() doesn't work?

It's important to clarify that doing jQuery('#open').click() does not execute the href attribute of an anchor tag so you will not be redirected. It executes the onclick event for #open which is not defined.

below code will get the desired output of window getting open on image click.

JQuery Code

$(document).ready(function(){
    $('.tmb').click(function(){
        var target=$(this).parent().find("a");

        $("#debug").text("clicked "+target.text());
        //target.triggerHandler('click');
        window.open(target.attr('href'));

    });
});​

Hope this helps, cheers!

In HTML4 the click event was defined only for the input element and some browser, most notably FF, strictly followed the specification. However HTML5 the game changed and any modern browser implements this functionality, but jQuery still has a special case for this event (extract from jquery trigger method):

if ( !onlyHandlers && !event.isDefaultPrevented() ) {

    if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) && !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {

    // Call a native DOM method on the target with the same name name as the event.

As a workaround you can change your code from:

target.trigger("click");

to

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