全局jQuery函数 [英] global jquery function

查看:105
本文介绍了全局jQuery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在最初加载的js文件中编写全局函数.我想在上面写函数,以便可以从所有页面访问它.我是jquery新手.我想知道如何在js文件中编写函数并在其他页面中调用它吗?

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 know how to write function in js file and call it form other pages?

推荐答案

您可以通过执行以下操作来添加自己的jQuery函数

You can add your own jQuery function by doing the following

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

然后在另一个脚本或相同脚本中,您可以通过执行$('#myId').MyFunction();来运行函数.这是为了在对象上运行功能.以下建议仅用于运行功能.

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.

将其添加到jQuery对象本身中:

Adding it to the jQuery object itself:

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

然后将其称为$.MyFunction()

这篇关于全局jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆