簡體   English   中英

阻止默認在Firefox中工作,但在chrome中不工作

[英]prevent default working in Firefox but not in chrome

點擊此鏈接

<a id="c2c" href="#" class='test' style="margin-left:300px;"><img src="img.png" height="400" width="480" alt="Click Here"></a>

我已經寫了這段代碼

$('#c2c').click(function(event){
    event.preventDefault();                     
        window.location.href = "http://google.com"; 
     $.get("test.php?testnum=121212121",function(data){
        if(data == "no") window.location.href = "failure.html";
        else {
               window.location.href = "http://www.abc.com/get_data.php?data="+data;
                     }              
});

點擊此鏈接,我是
1-采取默認行為
2-設置href位置
3-將帶有某些參數的獲取請求發送到另一頁
它們在FireFox中都工作正常,但是當我在Chrome / opera中嘗試使用此代碼時,它不是3- sending the get request並且在錯誤控制台中,我可以看到瀏覽器顯示“請求中止”。
有什么幫助嗎?

注意:據我調查,這是由於無法使用event.preventDefault();所致event.preventDefault(); ,如果我是對的,那我該如何使它正常工作?或者什么解決方案?

您必須刪除location.href:

('#c2c').click(function(event){
  event.preventDefault();                     
  // window.location.href = "http://google.com"; remove this line or move it somewhere in the response callback of your ajax request
  $.get("test.php?testnum=121212121",function(data){
    if(data == "no") {
      window.location.href = "failure.html";
    } else {
      window.location.href = "http://www.abc.com/get_data.php?data="+data;
    }              
});

Chrome終止了Ajax請求,因為window.location.href =“ http://google.com ”; 使瀏覽器加載google.com。 我無法為您提供完整的代碼示例,因為我不確定您要實現的目標

更改錨href

href="#"

href="javascript:void(0);"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM