2013-10-27 29 views
0

我只是想替換的onclick,負載文本,並與最終的文本替換:jQuery的更換:之前和.load功能後

$("#rebuild").click(function(){ 
    $("#rebuild").replaceWith("in progress...").load("/rebuild").replaceWith("end !"); 
}); 

的/重建腳本是否正確裝入但最終取代不顯示...爲什麼?

回答

2

load是一個異步AJAX方法。這意味着你的最終replaceWith從服務器上的數據被放置在元素之前發生。相同的元素鏈接replaceWith是喜客充其量,因爲一旦它取代了它不再存在,因此沒有什麼下一個replace

如果你想要更換後,你需要使用的'load

$("#rebuild").click(function() { 
    $(this).html("in progress...").load("/rebuild", function() { 
     setTimeout(function(){ 
      $(this).html("end !"); 
     }, 2000) 
    }) 
}); 
完整的回調

我用了一個setTimeout做最後的變化,所以它不是瞬時或整個負載是沒有意義的。還使用html()而非replaceWith所以元件仍然存在,並且只有它; S內容改變

1

http://api.jquery.com/load/

負載接受函數的自變量,一旦它是完整的,以及。

$("#rebuild").click(function(){ 
    $("#rebuild").replaceWith("in progress...").load("/rebuild", function(){ 
     $(this).replaceWith("end !"); 
    }); 
}); 

編輯:杜蘭,有人打我吧:P