使用.click()事件触发锚标记

Abh*_*nyu 0 jquery

对于我的应用程序,我想使用.click()事件触发锚标记,该事件应重定向到href中的页面提及.

我正在使用以下代码来实现它.但它没有按预期工作.

<!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="login_or_register">Login or register</a>
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

帮帮我!!

Nic*_*ver 9

您正在单击该链接,但这不会导致浏览器跟随链接("默认操作"或行为就是这个名称),所以不是这样:

$("#login_or_register").click();
Run Code Online (Sandbox Code Playgroud)

你需要这个:

window.location.href = 'abc.html';
//or dynamically:
window.location.href = $("#login_or_register").attr('href');
Run Code Online (Sandbox Code Playgroud)