我在script.js
文件中有这个功能:
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;
}
});
}
alert()
运行前each
,但其他2运行不运行。
除非,我在调试模式(Firefox)中运行它,其中一切都按预期运行。
在调试模式下,我看到voiceintelements
具有正确的元素选择以及each
如何正确循环它们。
如果有帮助,我就可以通过我的loadMarqueeHTMLs
文件调用index.html
函数:
<script>
$(document).ready(function() {
loadMarqueeHTMLs();
});
</script>
答案 0 :(得分:0)
您需要等待加载voiceInteractions,当您运行此var 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;
});
}
});
});
}