开发者

trigger an anchor tag using the .click() event

For my application, I want to trigger an anchor tag using the .click() event, which should redirect to page mention in href.

I'm using following code to achieve it.But its not working as expected.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title></title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>
    <script>
    $(function() {
        $('#upload').click(function(e) {
            e.preventDefault();
                $("#login_or_register").click();
        });
    });
    </script>
    <div id ='upload'>upload</div>
    <a  style ='display:none' href="abc.html" id开发者_C百科="login_or_register">Login or register</a>
    </body>
    </html>

Help me out!!


You are clicking the link, but that won't cause the browser to follow the link (the "default action" or behavior is what this is called), so instead of this:

$("#login_or_register").click();

You need this:

window.location.href = 'abc.html';
//or dynamically:
window.location.href = $("#login_or_register").attr('href');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜