2015-10-14 70 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

相关问题