的Javascript仅在调试模式执行
问题描述:
我在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>
答
你需要等待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;
});
}
});
});
}
您需要等待voiceInteractions加载。 – Omidam81
为jQuery加载创建一个回调函数,然后运行其中的代码。 – mehulmpt
请检查答案。 – Omidam81