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

如何調用嵌套在 JQuery 插件中的函數?

How to call functions that are nested inside a JQuery Plugin?(如何調用嵌套在 JQuery 插件中的函數?)
本文介紹了如何調用嵌套在 JQuery 插件中的函數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我的目標是能夠調用我的 JQuery 插件中的函數.

My goal is to be able to call functions that are inside my JQuery plugin.

什么是正確的語法?

例如,這不起作用:

<a href="#" id="click_me">Click Me</a>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
(function($) { 
    $.fn.foo = function(options) {
        do_stuff = function(){
            console.log("hello world!"); // works
            do_other_stuff = function(){
            alert("who are you?");
            }
        } // function
    } // function
})(jQuery);

$("body").foo();

$("#click_me").click(function(){
$.fn.foo.do_stuff.do_other_stuff(); // doesn't work
});

</script>

推薦答案

當您將函數分配給沒有 var 關鍵字的變量時,它們要么覆蓋該名稱的局部變量,要么被添加到全局命名空間中.(所以你的 do_stuff 是一個全局函數,這不是你想要的)

when you assign functions to variables without the var keyword they either overwrite the local variable of that name or the are added to the global namespace. (so your do_stuff is a global function, which is not what you want)

做你想做的事的一種方法是明確地給出你希望你的函數駐留在哪里.

one way to do what you want is to explicitly give where you want your function to reside.

(function($) { 
    $.fn.foo = function(options) {
        // whatever $().foo() should do
    };

    $.fn.foo.do_stuff = function() {
        console.log("hello world!");
    };

    $.fn.foo.do_stuff.do_other_stuff = function(){
        alert("who are you?");
    };
})(jQuery);

編輯:

這是因為 javascript 中的所有函數都是對象,這意味著您可以為任意屬性賦值.

This works because all functions in javascript are objects, which means you can assign values to any arbitrary property.

如果您想訪問其他函數的變量,您可以將定義移到其他函數中,例如:

If you want to access the variables of other functions you can move the definitions inside of the other ones like:

$.fn.foo.do_stuff = function() {
    console.log("hello world!");
    $.fn.foo.do_stuff.do_other_stuff = function(){
        alert("who are you?");
    };
};

但這意味著該函數僅在您運行另一個函數時才定義,并且每次運行該函數時它都會覆蓋最后一個定義.

but this will mean the function is only defined once you run the other function, and that each time you run the function it will overwrite the last definition.

可能更理想的解決方案是讓每個函數返回一個包含嵌套函數的對象,如下所示:

Possibly a more ideal solution would be to have each function return an object containing the nested function like so:

(function($) { 
    $.fn.foo = function(options) {
        // whatever $().foo() should do

        var do_stuff = function(do_stuff_args) {
            console.log("hello world!");
            // do stuff with options and do_stuff_args

            var do_other_stuff = function(other_args) {
                alert("who are you?");
                // here you have access to options, do_stuff_args, and other_args
            };

            return {
                do_other_stuff: do_other_stuff
            };
        };

        return {
            do_stuff: do_stuff
        }
    };
})(jQuery);

并使用它調用它

foo().do_stuff(some_options).do_other_stuff(other_options);

var a = foo(stuff).do_stuff(some_options);
a.do_other_stuff(foo);
a.do_other_stuff(bar);

這篇關于如何調用嵌套在 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 | 久久久123 | 国产一区二区三区久久久久久久久 | 一区网站| 国产精品欧美精品日韩精品 | 成人福利电影 | 综合久久亚洲 | 国产免费一二三区 | 999久久 | 亚洲欧洲视频 | 日韩一区二区在线视频 | 国产精品久久久久aaaa九色 | 欧美亚洲另类丝袜综合网动图 | 欧美日韩不卡合集视频 | 欧美精品一区二区三区四区五区 | 成人深夜福利 | 国产精品99久久久久久久vr | 亚洲免费三区 | 喷潮网站| 在线免费观看黄视频 | 欧洲色综合| 天天操天天天 | 国产传媒毛片精品视频第一次 | 91视频一区二区三区 | 亚洲第一女人av | 黄色片免费 | 国产精品欧美一区二区 | 欧美日在线 | 欧美国产日韩在线观看 | 免费一级欧美在线观看视频 | 精品视频久久久久久 | av一二三区| 精品久久国产 | 久久久久久国产精品 | 中文字幕免费中文 | 亚洲精选久久 | 国产精品亚洲一区 | 日韩中文字幕一区二区 |