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

在不指定任何元素的情況下調用 jQuery 插件

Calling a jQuery plugin without specifying any elements(在不指定任何元素的情況下調用 jQuery 插件)
本文介紹了在不指定任何元素的情況下調用 jQuery 插件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有以下 jQuery 插件:

Say I have the following jQuery plugin:

$.fn.myPlugin = function () {
    //plugin code
}

通常,您在一定數量的元素上調用插件,例如:

Normally, you call a plugin on a certain number of elements, like such:

$("body").myPlugin();

<小時>

有什么方法可以在不指定元素的情況下調用我的插件?


Is there any way I can call my plugin without specifying an element?

我試過這樣稱呼它:$.myPlugin();,但這不起作用.

I have tried calling it like such: $.myPlugin();, but that does not work.

什么是這樣:$().myPlugin();,但這是調用它的正確方法嗎?

What works is this: $().myPlugin();, but is that the correct way to invoke it?

推薦答案

快速的寫法是這樣的:

$.myPlugin = function () {
    // Plugin code
}

正確的寫法是這樣的:

(function ($) {
    $.extend({
        myPlugin: function () {
            // plugin code
        }
    });
})(jQuery);

一開始可能有點混亂,但這是一個常見的 jQuery 模式.

It might seem a little confusing at first, but it's a common jQuery pattern.

(function($){
     // Code
})(jQuery);

這段代碼創建了一個匿名函數并通過 jQuery 作為參數調用它.在函數內部,此參數綁定到 $.這樣做的原因是它允許您使用 $ 即使 jQuery 在 無沖突模式.

This code creates an anonymous function and calls it passing jQuery as argument. Inside the function this argument is bound to $. The reason this is done it that it allows you to work with the $ even if jQuery is running in no-conflict mode.

第二部分是$.extend.當使用單個參數調用時,它基本上擴展了 jQuery 對象本身.

The second part is $.extend. It basically extends the jQuery object itself, when called with a single argument.

調用插件(在快速且正確的情況下)是:

Calling the plugin (in the quick and the right case) is:

$.myPlugin();

這篇關于在不指定任何元素的情況下調用 jQuery 插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

jQuery/JavaScript Library for avatar creation?(用于創建頭像的 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 - 為垂直滾動網站配置偏移量/對齊元素?)
jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 国产激情自拍视频 | 91精品国产色综合久久 | 欧美video | 国产一区 日韩 | 成人午夜免费福利视频 | 久久99国产精品久久99果冻传媒 | 久久精品这里 | 久久精品免费一区二区 | 天天成人综合网 | 一区二区三区欧美在线 | 国产成人久久av免费高清密臂 | 亚洲一区国产 | 天天看天天操 | 日韩精品视频在线免费观看 | 91精品国产综合久久福利软件 | 午夜影视免费片在线观看 | 中文字幕在线一区二区三区 | 欧美国产日韩一区二区三区 | 色资源站| 欧美一二三 | 日韩在线精品强乱中文字幕 | 欧美高清一级片 | 久久久久久成人 | 最新伦理片 | 亚洲国产精品99久久久久久久久 | 亚洲va欧美va天堂v国产综合 | 亚洲精品久久久9婷婷中文字幕 | 在线看片网站 | 国产精品99久久免费观看 | 日韩色视频 | 日韩中文在线观看 | 久草青青草 | 亚洲国产二区 | 欧美久久一级特黄毛片 | 亚洲精彩免费视频 | 久久久久久国产 | 福利影院在线看 | 成人黄色在线 | 国产免费人成xvideos视频 | 久在线| 狠狠插狠狠操 |