[英]Avoiding Javascript Function expected error on implementing document ready function
I have a component on my page that needs to be hidden until the documented is ready. 我的页面上有一个组件需要隐藏,直到准备好文档为止。 Therefore I have defined a couple of DIVs and implemented the document ready function:
因此,我定义了几个DIV并实现了文档准备功能:
<div id="LoadingDiv" style="display:block;text-align:center">
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="form-horizontal" id="LoadedDiv" style="display:none;">
...
</div>
<script type="text/javascript">
$(document).ready(function () {
setTimeout(function () {
document.getElementById("LoadingDiv").style.display = "none";
document.getElementById("LoadedDiv").style.display = "block";
}, 500);
})
$(document).on("change", function () {
window.onbeforeunload = function () {
return "your changes to this form have not been saved";
};
})
</script>
Whilst this seems to work, it generates 'Function Expected' error in the browser's developer console. 尽管这似乎可行,但它会在浏览器的开发人员控制台中生成“预期功能”错误。 I've tried various different ways of expressing the function - see below.
我尝试了多种表达功能的方法-参见下文。 I've also tried replacing the function body with something simple like alert("me").
我还尝试过用Alert(“ me”)之类的简单东西替换函数主体。 The error goes away if I remove the 'ready' function, but keep the document 'change' function.
如果删除“就绪”功能,但保留文档“更改”功能,该错误就会消失。 Therefore I'm now quite sure the problem lies in the 'ready' function definition.
因此,我现在可以确定问题出在“就绪”函数定义中。
By the way, I'm using JQuery 2.2.0 with Bootstrap 3.3.6. 顺便说一句,我使用的是带有Bootstrap 3.3.6的JQuery 2.2.0。
You forgot to add ; 你忘了加; at the end of function.
在功能结束时。 in both function.
在两个功能上。
$(document).ready(function () {
setTimeout(function () {
document.getElementById("LoadingDiv").style.display = "none";
document.getElementById("LoadedDiv").style.display = "block";
}, 500);
}); //added ; at the end of function.
Try replacing this code with your code... 尝试将此代码替换为您的代码...
Well spotted Gaurang! 发现高良! I need new spectacles ;-) The correct code is
我需要新眼镜;-)正确的代码是
<div id="LoadingDiv" style="display:block;text-align:center">
<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i>
</div>
<div class="form-horizontal" id="LoadedDiv" style="display:none;">
...
</div>
<script type="text/javascript">
$(document).ready(function () {
setTimeout(function () {
document.getElementById("LoadingDiv").style.display = "none";
document.getElementById("LoadedDiv").style.display = "block";
}, 500);
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.