久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

jQuery 插件命名空間函數(shù)

jQuery Plugin Namespacing Functions(jQuery 插件命名空間函數(shù))
本文介紹了jQuery 插件命名空間函數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在創(chuàng)建一個范圍相當大的 jQuery 插件.事實上,從技術上講,該插件由幾個共同工作的插件組成.

I'm creating a jQuery plugin that that is rather large in scope. In fact, the plugin technically consists of a few plugins that all work together.

(function($){
    $.fn.foo = function(){
        //plugin part A
    }
    $.fn.bar = function(){
        //plugin part B
    }
    $.fn.baz = function(){
        //plugin part C
    }
}(jQuery))

是否可以為 jQuery 插件命名,以便次要插件可以是較大插件的功能

Is it possible to namespace jQuery plugins such that the minor plugins could be functions of the larger plugin

$.fn.foo.bar = function(){}
$.fn.foo.baz = funciton(){}

這樣可以避免污染 jQuery 函數(shù)命名空間.然后你可以調(diào)用插件像

This would keep from polluting the jQuery function namespace. You could then call the plugins like

$('#example').foo()
$('#other_example').foo.bar()

我自己嘗試這個時遇到的問題是聲明為 foo() 插件函數(shù)的屬性的函數(shù)沒有正確設置對this"的引用.'this' 最終指向父對象而不是 jQuery 對象.

The issue I have run into when trying this out myself is that the functions declared as properties of the foo() plugin function don't have their references to 'this' set properly. 'this' ends up referring to the parent object and not the jQuery object.

任何想法或意見將不勝感激.

Any ideas or opinions would be appreciated.

-馬特

推薦答案

只要你使用 $.fn.foo.bar() -- this 指向$.fn.foo,這是您在 JavaScript 中所期望的(this 是調(diào)用函數(shù)的對象.)

As soon as you use $.fn.foo.bar() -- this points to $.fn.foo, which is what you would expect in JavaScript (this being the object that the function is called on.)

我注意到 jQuery UI 的插件(如可排序)中調(diào)用的函數(shù)如下:

I have noticed in plugins from jQuery UI (like sortable) where you call functions like:

$(...).sortable("serialize");
$(...).sortable({options});

如果你正在做這樣的事情 - 你可以擴展 jQuery 本身:

If you were doing something like this - you could extend jQuery itself:

$.foo_plugin = {
  bar: function() {},
  baz: function() {}
}

$.fn.foo = function(opts) {
  if (opts == 'bar') return $.foo_plugin.bar.call(this);
  if (opts == 'baz') return $.foo_plugin.baz.call(this);
}

這篇關于jQuery 插件命名空間函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創(chuàng)建頭像的 jQuery/JavaScript 庫?)
How to do following mask input problem?(如何做以下掩碼輸入問題?)
Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 設置值/標簽的問題)
how to unit-test private methods in jquery plugins?(如何對 jquery 插件中的私有方法進行單元測試?)
stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 為垂直滾動網(wǎng)站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內(nèi)容)
主站蜘蛛池模板: 亚洲精品一二区 | 日日爱夜夜操 | 天天搞天天操 | 亚洲精品电影网在线观看 | 亚洲免费毛片 | 国产一区二区精品在线 | 欧美日韩一区不卡 | 日韩在线视频观看 | 九九久久久久久 | 国产成人精品免高潮在线观看 | 久久精品福利 | 国产黄色一级电影 | 一区二区av | 精品视频一区二区在线观看 | 999久久久久久久久 国产欧美在线观看 | www.精品一区 | 日本特黄a级高清免费大片 国产精品久久性 | 美国av片在线观看 | 欧美日韩亚洲一区 | 91在线精品视频 | 美女久久 | 久久综合一区二区 | 亚洲免费精品 | 91视频大全 | www狠狠干 | 欧美成人在线网站 | av在线播放一区二区 | 成人影院免费视频 | 久久国产婷婷国产香蕉 | 亚洲欧美一区二区三区视频 | 国产精品成人一区二区三区吃奶 | 涩爱av一区二区三区 | 欧美福利精品 | 国产精品久久久99 | 国产一区二区在线免费 | 国产乱码精品1区2区3区 | 久久久成人网 | 人人九九精 | 999精品视频在线观看 | 麻豆va| 天天干.com |