[英]Is there a way to use jQuery functions if they are not defined in the `$(document),ready` function?
I have several scripts and the $(document).ready
is being defined in another script.我有几个脚本,并且
$(document).ready
正在另一个脚本中定义。 I tried defining functions in another file that make use of JQuery syntax but they don't work.我尝试在另一个文件中定义使用 JQuery 语法的函数,但它们不起作用。 Is there a way to use jQuery functions if they are not defined in the
$(document),ready
function?如果 jQuery 函数没有在
$(document),ready
中定义,有没有办法使用 function?
Here is my first script.这是我的第一个脚本。 it's just a ready block (yes its empty):
它只是一个就绪块(是的,它是空的):
$(document).ready(function () {
});
This is my second script:这是我的第二个脚本:
function editPage() {
$("#mybutton").click(function() {
alert("Clicked");
});
}
editPage();
It does not work.这没用。
The second code does not work.第二个代码不起作用。 They are in different files.
它们位于不同的文件中。 The first script is always loaded.
第一个脚本总是被加载。 I have the jquery library defined.
我定义了 jquery 库。 If i place the
click
code in the first script, it works.如果我将
click
代码放在第一个脚本中,它就可以工作。
This is ASP.NET MVC 5这是 ASP.NET MVC 5
If I understand you correct, as long as you reference JS file earlier, you can use it in all other files which is coming later in sequence.如果我理解你是正确的,只要你之前引用了 JS 文件,你就可以在后面的所有其他文件中使用它。
In following scenario, you can use Jquery in "your-test-file.js"在以下场景中,您可以在“your-test-file.js”中使用 Jquery
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="/your-test-file.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.