简体   繁体   English

我必须在外部脚本中包含$(document).ready(function()多少次

[英]how many times do I have to include the $(document) .ready (function() in my external script

I'm having trouble figuring out how to link my javascript externally. 我在弄清楚如何在外部链接我的JavaScript时遇到了麻烦。 I'm not sure what the issue is but inline the script works fine. 我不确定是什么问题,但是内联脚本可以正常工作。 However, when I throw the script into a .js file and try to link it from a <script type="text/javascript"> it doesn't work at all. 但是,当我将脚本放入.js文件并尝试从<script type="text/javascript">它根本不起作用。

I have several instances of the same function going on (because it's something that occurs in multiple sections, which is different content but the same type of function of a hidden div being shown when another element is clicked). 我有几个执行相同功能的实例(因为它发生在多个部分中,这是不同的内容,但是单击另一个元素时会显示隐藏的div的相同类型的功能)。

I don't know if it's because I'm including $(document).ready(function() too many times or not enough, or what. I HAVE to place the <script type="text/javascript"> in the body because I do not have access to the head section of my pages (editing inside of a WSIWYG EzPublisher mce type editor). 我不知道这是否是因为我多次包含$(document).ready(function()还是不够,或者什么。我必须在<script type="text/javascript">放置<script type="text/javascript">因为我无权访问页面的头部(在WSIWYG EzPublisher mce类型编辑器中进行编辑)。

My script is too long to place in stackoverflow, but I'll perhaps post it on a web server that I do control if need be. 我的脚本太长了,无法放置在stackoverflow中,但是我可能会将其发布到我需要控制的Web服务器上。

But my nutshell question is how many times should I be including the $(document) .ready(function() in my external .js file? 但是我的问题是,我应该在外部.js文件中包含$(document) .ready(function()多少次?

One Time is enough , if you add more than one time then all of them all be executed, so it really doesnt matter . 一个时间就足够了,如果您添加了一个以上的时间,那么所有这些时间都将被执行,所以这真的没有关系。

in other words you are just adding more handlers to "ready" event. 换句话说,您只是向“就绪”事件添加了更多处理程序。

and if you dont have any code to be inside "ready" event then no need to add it at all 并且如果您没有任何代码包含在“就绪”事件中,则根本无需添加任何代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM