2012-04-13 52 views
0

我做了哪些該做的3個步驟爲什麼Jquery .load()函數沒有成功激發?

  1. onload事件就應更換所有鏈接文件的屬性的jQuery函數
  2. 單擊它,應該在子頁面加載到$(「#內容」)時
  3. 它再次重燒將取代所有鏈接的功能再次

下面的代碼工作,直到2個步驟,但犯規防火第三步,我想這也應該更換加載頁面的鏈接應該再次更換所有的網址?

$(document).ready(function(){ 
    var base_url = "http://localhost/dolphin/"; 
    $("a").each(function(e){ 
    if(this.href == "javascript:void(0)" || 
     this.href == base_url || 
     this.href == base_url + "index.php" || 
     this.href == "javascript:void(0);" || 
     this.href == "javascript:%20void(0)") { 

    }else{ 
     page = 'Javascript:LoadPage(\''+ this.href + '\')'; 
     this.setAttribute('onclick',page); 
     this.setAttribute('href', '#'); 
    } 
    }); 
}); 

function LoadPage(url){ 
    $("#contentarea").load(url, function() { 
    //after loading it should call redoIt() but it doesnt. 
     redoIt(); 
    }); 
} 

function redoIt(){ 
    var base_url = "http://localhost/dolphin/"; 
    $("a").each(function(e){ 
     if(this.href == "javascript:void(0)" || 
      this.href == base_url + "index.php" || 
      this.href == "javascript:void(0);" || 
      this.href == "javascript:%20void(0)") { 

     }else{ 
     page = 'Javascript:LoadPage(\''+ this.href + '\')'; 
     this.setAttribute('onclick',page); 
     this.setAttribute('href', '#'); 
     } 
    }); 
} 
+0

你怎麼知道'redoIt()'沒有被調用?你有沒有在其中放置一個斷點?或者在開始時使用'console.log()'語句?錯誤控制檯中是否有任何javascript錯誤?你確定LoadPage()被調用嗎? – jfriend00 2012-04-13 15:10:30

+0

你的選擇鏈接的方式真是太可怕了......長久以來,還沒有看到jQuery的這種可怕的濫用。 – ThiefMaster 2012-04-13 15:12:15

+0

我在redoIt()中添加了alert(this.href),但沒有回覆 – danny 2012-04-13 15:13:03

回答

1

您對$.load調用一個函數裏面,從我收集你不叫任何地方,所以它沒有得到執行。拿出來,它應該很好

$("#contentarea").load(url, function() { 

//after loading it should call redoIt() but it doesnt. 
redoIt(); 
});