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

如何使用方法創(chuàng)建 jQuery 插件?

How to create a jQuery plugin with methods?(如何使用方法創(chuàng)建 jQuery 插件?)
本文介紹了如何使用方法創(chuàng)建 jQuery 插件?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試編寫一個(gè) jQuery 插件,該插件將為調(diào)用它的對(duì)象提供額外的功能/方法.我在網(wǎng)上閱讀的所有教程(過去2小時(shí)一直在瀏覽)最多包括如何添加選項(xiàng),而不是附加功能.

I'm trying to write a jQuery plugin that will provide additional functions/methods to the object that calls it. All the tutorials I read online (have been browsing for the past 2 hours) include, at the most, how to add options, but not additional functions.

這是我想做的事情:

//通過調(diào)用該div的插件將div格式化為消息容器

//format div to be a message container by calling the plugin for that div

$("#mydiv").messagePlugin();
$("#mydiv").messagePlugin().saySomething("hello");

或類似的東西.歸結(jié)為:我調(diào)用插件,然后調(diào)用與該插件關(guān)聯(lián)的函數(shù).我似乎無法找到一種方法來做到這一點(diǎn),而且我之前已經(jīng)看到很多插件都這樣做了.

or something along those lines. Here's what it boils down to: I call the plugin, then I call a function associated with that plugin. I can't seem to find a way to do this, and I've seen many plugins do it before.

這是我目前所擁有的插件:

Here's what I have so far for the plugin:

jQuery.fn.messagePlugin = function() {
  return this.each(function(){
    alert(this);
  });

  //i tried to do this, but it does not seem to work
  jQuery.fn.messagePlugin.saySomething = function(message){
    $(this).html(message);
  }
};

我怎樣才能實(shí)現(xiàn)這樣的目標(biāo)?

How can I achieve something like that?

謝謝!

2013 年 11 月 18 日更新:我已將正確答案更改為 Hari 以下評(píng)論和點(diǎn)贊的答案.

Update Nov 18, 2013: I've changed the correct answer to that of Hari's following comments and upvotes.

推薦答案

根據(jù) jQuery Plugin Authoring 頁面 (http://docs.jquery.com/Plugins/Authoring),最好不要混淆 jQuery 和 jQuery.fn 命名空間.他們建議采用這種方法:

According to the jQuery Plugin Authoring page (http://docs.jquery.com/Plugins/Authoring), it's best not to muddy up the jQuery and jQuery.fn namespaces. They suggest this method:

(function( $ ){

    var methods = {
        init : function(options) {

        },
        show : function( ) {    },// IS
        hide : function( ) {  },// GOOD
        update : function( content ) {  }// !!!
    };

    $.fn.tooltip = function(methodOrOptions) {
        if ( methods[methodOrOptions] ) {
            return methods[ methodOrOptions ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof methodOrOptions === 'object' || ! methodOrOptions ) {
            // Default to "init"
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  methodOrOptions + ' does not exist on jQuery.tooltip' );
        }    
    };


})( jQuery );

基本上,您將函數(shù)存儲(chǔ)在數(shù)組中(范圍為包裝函數(shù)),如果傳遞的參數(shù)是字符串,則檢查條目,如果參數(shù)是對(duì)象(或空).

Basically you store your functions in an array (scoped to the wrapping function) and check for an entry if the parameter passed is a string, reverting to a default method ("init" here) if the parameter is an object (or null).

然后你可以像這樣調(diào)用方法......

Then you can call the methods like so...

$('div').tooltip(); // calls the init method
$('div').tooltip({  // calls the init method
  foo : 'bar'
});
$('div').tooltip('hide'); // calls the hide method
$('div').tooltip('update', 'This is the new tooltip content!'); // calls the update method

Javascript 的參數(shù)"變量是一個(gè)包含所有傳遞參數(shù)的數(shù)組,因此它適用于任意長度的函數(shù)參數(shù).

Javascripts "arguments" variable is an array of all the arguments passed so it works with arbitrary lengths of function parameters.

這篇關(guān)于如何使用方法創(chuàng)建 jQuery 插件?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進(jìn)程中創(chuàng)建子窗口時(shí)如何修復(fù) BrowserWindow 不是構(gòu)造函數(shù)錯(cuò)誤) - IT屋-程序員軟件開發(fā)技術(shù)
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應(yīng)用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個(gè) loadURL 上執(zhí)行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內(nèi)使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲(chǔ)?)
How to access camera/webcamera inside electron app?(如何在電子應(yīng)用程序中訪問相機(jī)/網(wǎng)絡(luò)攝像頭?)
主站蜘蛛池模板: 乳色吐息在线观看 | 一级黄色大片 | 不卡的av在线 | 欧产日产国产精品99 | 久久国产高清 | 日韩成人在线播放 | 国产成人精品一区二区三区网站观看 | 色必久久 | 精久久| 国产精品久久久久久久久久久久 | 罗宾被扒开腿做同人网站 | 一区二区久久电影 | 日日摸夜夜添夜夜添精品视频 | 亚洲国产网址 | 亚洲第一中文字幕 | 91视在线国内在线播放酒店 | 日韩成人在线播放 | 国产av毛片| 国产综合精品 | 欧美电影大全 | 新疆少妇videos高潮 | 国内精品久久影院 | 久久久精品国产 | 中文字幕在线电影观看 | 久一精品 | 久草网站 | 日韩在线一区二区三区 | 久久综合一区二区三区 | 国产成人综合网 | 欧美人妇做爰xxxⅹ性高电影 | 精品免费视频 | 色吊丝2| 久久国内 | www.亚洲免费 | 国产成人一区二区三区久久久 | 欧美一级在线视频 | 日韩色在线 | 亚洲精品乱码久久久久久黑人 | 在线成人av | 精品自拍视频在线观看 | 国产精品18hdxxxⅹ在线 |