global jquery function

后端 未结 2 1543
-上瘾入骨i
-上瘾入骨i 2021-01-17 22:37

I have to write global function in js file that is loaded initially. I want to write function on it so that it can be accessed from all pages. I am new in jquery. I want to

2条回答
  •  时光取名叫无心
    2021-01-17 23:34

    You can add your own jQuery function by doing the following

    $.fn.MyFunction = function()
    {
    //Code here
    }
    

    Then inside of another script or the same script, you can run your function by doing $('#myId').MyFunction();. This is for running the function on an object. The suggestion below is for just running a function.

    Adding it to the jQuery object itself:

    $.MyFunction = function()
    {
    //Code here
    }
    

    Then call it as $.MyFunction()

提交回复
热议问题