2015-06-16 29 views
0
<a id="link" href="http://google.com">Let's Go</a> 

$("#link").on("click", function(){ 

    $.post("process.php", 
        { 
         x: pos.x, 
         y: pos.y 
        }, 
        function(data, status){ 
         // do something here 
       }); 
      }); 

    }); 

當點擊Let's Go鏈接時會發生什麼?瀏覽器是否等待發送請求並從process.php頁獲取結果?或忘記請求並立即關注鏈接?當我在鏈接上發送ajax請求時會發生什麼?

回答

0

href會覆蓋任何JS函數。所以在你的情況下,它只會重定向到http://google.com。如果你想讓它忽略href,那麼你需要使用這樣的e.preventDefault()

$("#link").on("click", function(e){ 
    e.preventDefault(); 
    //... more code 
}); 
相關問題