2015-10-14 64 views
0

我在script.js文件這一功能:的Javascript僅在調試模式執行

function loadMarqueeHTMLs(){ 
    $("#voiceInteractions").load("xmlTest1A.html"); 
    $("#nonvoiceInteractions").load("xmlTest1B.html"); 
    var voiceintelements = $("#voiceInteractions").find(".value"); 
    alert("Before each"); // this alert runs 
    voiceintelements.each(function() { 
     alert($(this).attr('id')); // this alert DOESN'T run 
     switch ($(this).attr('id')) { 
      case "intsWaiting": 
       alert("inside intsWaiting"); // this alert DOESN'T run 
       break; 
      default: 
       break; 
     } 
    }); 
} 

each運行之前alert(),但其他2不運行。 除非,我在調試模式(Firefox)中運行它,其中一切按預期運行。 在調試模式下,我看到voiceintelements有正確的元素選擇以及each如何正確循環它們。

萬一有幫助,這就是我所說的loadMarqueeHTMLs功能從我index.html文件:

<script> 
$(document).ready(function() { 
    loadMarqueeHTMLs(); 
}); 
</script> 
+0

您需要等待voiceInteractions加載。 – Omidam81

+0

爲jQuery加載創建一個回調函數,然後運行其中的代碼。 – mehulmpt

+0

請檢查答案。 – Omidam81

回答

0

你需要等待voiceInteractions加載,當你運行該變種不加載voiceintelements = $("#voiceInteractions").find(".value");$("#voiceInteractions")內容,以便voiceintelements = $("#voiceInteractions").find(".value")是空的

function loadMarqueeHTMLs(){ 
    alert("Before each"); // this alert runs 
    $("#voiceInteractions").load("xmlTest1A.html", function(){ 
      $("#nonvoiceInteractions").load("xmlTest1B.html", function(){ 
       var voiceintelements = $("#voiceInteractions").find(".value"); 
       voiceintelements.each(function() { 
       alert($(this).attr('id')); // this alert DOESN'T run 
       switch ($(this).attr('id')) { 
        case "intsWaiting": 
          alert("inside intsWaiting"); // this alert DOESN'T run 
          break; 
         default: 
          break; 
       }); 
     } 
     }); 
    }); 


} 
+0

太棒了!現在,我想再次爲我的下一個加載調用相同的函數。這樣做的最佳實踐方式是什麼? – dzmr83

+0

代碼已更新。請檢查一下。你在第一個負載回調的回調中調用load。 – Omidam81