简体   繁体   English

如果 function 中没有定义 jQuery 函数,有没有办法使用它们?

[英]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.

相关问题 在jQuery中使用$(document).ready()的可靠方法 - Reliable way to use $( document ).ready() in jQuery 未在document.ready jQuery代码中定义的Javascript函数 - Javascript function not defined within document.ready jquery code 尝试从 jquery $(document).ready 调用外部 js 中定义的 function - Try to call function defined in external js from jquery $(document).ready $(document).ready和jQuery(function($)不起作用;已定义jQuery,但脚本未输入.ready函数 - $(document).ready and jQuery(function($) not working; jQuery is defined, but script isn't entering .ready function jQuery 文档就绪功能不起作用 - jQuery document ready functions not working 在jQuery中document.ready函数外部的事件上调用函数 - Call functions on events outside document.ready function in jquery 如何在 document.ready 上调用多个 js 函数而不放置它们 jQuery(document).ready(function(){}); - How to call multiple js functions on document.ready without putting them jQuery(document).ready(function(){}); 从$(document).ready之外的函数调用到$(document).ready中定义的函数 - Function calls from outside of $(document).ready to the function defined in the $(document).ready 在文档就绪的jQuery函数上使用ajax success:function(data) - use ajax success:function(data) on document ready jquery function 如果我使用 jQuery(document).ready(function(){ ... my code}) = undifined function - If i use jQuery(document).ready(function(){ … my code}) = undifined function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM