简体   繁体   中英

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. I tried defining functions in another file that make use of JQuery syntax but they don't work. Is there a way to use jQuery functions if they are not defined in the $(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. If i place the click code in the first script, it works.

This is 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.

In following scenario, you can use Jquery in "your-test-file.js"

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="/your-test-file.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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