2010-08-12 27 views
0

對於我的應用程序,我想使用.click()事件觸發錨點標記,該事件應該重定向到href中的頁面提及。使用.click()事件觸發錨點標記

我使用下面的代碼來實現它,但它沒有按預期工作。

<!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> 

幫幫我!

+0

什麼問題? – prodigitalson 2010-08-12 19:25:32

+0

我已編輯過該問題,請檢查。 – Abhimanyu 2010-08-12 19:28:19

回答

8

你點擊該鏈接,但不會導致瀏覽器跟隨鏈接(「默認動作」或行爲是什麼,這是所謂的),所以不是這樣的:

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

你需要這個:

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