扩展jQuery是行不通的......? [英] Extending jQuery is not working...?

查看:88
本文介绍了扩展jQuery是行不通的......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到如何扩展jQuery ...我有以下代码:

I'm not getting how to extend jQuery... I have the following code:

jQuery.fn.extend({

    whatever : function () {
        alert('yeah');
    }

});

哪个应该给我一个 $。无论功能,没有?但是当我检查它,或者尝试运行它时:zilch。

Which should be giving me a $.whatever function, no? But when I check it, or try to run it: zilch.

看看这个小提琴:
http://jsfiddle.net/66gpf/

检查了文档,这对我来说似乎很好......所以,我做错了什么?

I checked the docs and it seems good to me... SO, what am I doing wrong??

推荐答案

jQuery.fn 是对 jQuery.prototype 。因此,当您执行 jQuery.fn.extend 时,您将属性添加到原型到jQuery本身。 所有函数都存在于所有jQuery实例上。

jQuery.fn is a reference to jQuery.prototype. So when you do jQuery.fn.extend, you adding a property to the prototype, not to jQuery itself. The whatever function will exist on all jQuery instances.

jQuery.fn.extend({
    whatever : function () {
        alert('yeah');
    }
});

$('#a_selector').whatever();

文档: http://api.jquery.com/jquery.fn.extend/

PS如果您希望函数以 $。无论而不是仅存在于jQuery对象实例上,那么将其添加到 $ 而不是 $。fn

P.S. If you want the function to exist as $.whatever instead of only on jQuery object instances, then add it to $ instead of $.fn.

jQuery.extend(jQuery, {
    whatever : function () {
        alert('yeah');
    }
});

$.whatever();

这篇关于扩展jQuery是行不通的......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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