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

從外部調用 jQuery 插件內部的函數

Calling a function inside a jQuery plugin from outside(從外部調用 jQuery 插件內部的函數)
本文介紹了從外部調用 jQuery 插件內部的函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試研究如何從插件外部調用我的 jQuery 插件中的函數.我嘗試過的代碼不起作用.我確定我將不得不重組我的插件以允許這樣做,但我不確定如何去做.在這個例子中,我試圖訪問 underline() 函數.

I am trying to work out how to call functions within my jQuery plugin from outside the plugin. The code I have tried is not working. I'm sure I will have to restructure my plugin to allow this, but I'm not sure how to. In this example, I'm trying to access the underline() function.

jsFiddle

jQuery 插件

(function($) {
    "use strict";

    $.fn.testPlugin = function(options) {

        // Settings
        var settings = $.extend({
            newText : "Yabadabado"
        }, options);

        return this.each(function(i, el) {            

            var init = function(callback) {
                if( $(el).attr("class") === "red" ) {
                    $(el).css("color","red");
                }

                $(el).text(settings.newText);

                if( callback && typeof(callback) === "function" ) {
                    callback();
                }
            };

            var underline = function() {
                $(el).addClass("underline");
            };

            init();
        });
    };

}(jQuery));

將插件分配給選擇器

var doTest = $("#testItem").testPlugin({
    newText: "Scoobydoo"
});

var doNewTest = $("#newTestItem").testPlugin({
    newText: "kapow!"
});    

調用插件中的函數

$("#underline").click(function(e) {
    e.preventDefault();
    doTest.underline();
});

推薦答案

看看 關閉.

這是一個基本示例,說明 jQuery 插件中的閉包.

Here is a basic example of what a closure looks like in a jQuery plugin.

$.fn.plugin = function() {

    return {
        helloWorld: function() {
            console.log('Hello World!');
        }
    }
};

// init plugin.
var test = $('node').plugin();

// call a method from within the plugin outside of the plugin.
test.helloWorld();

你可以在下面的 jsfiddle 看到另一個例子.

You can see another example at the following jsfiddle.

http://jsfiddle.net/denniswaltermartinez/DwEFz/

這篇關于從外部調用 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 屏蔽輸入插件.當文本框獲得焦點時選擇所有內容)
主站蜘蛛池模板: 国产一区2区 | 在线免费黄色 | 一级全黄少妇性色生活免费看 | 美美女高清毛片视频免费观看 | 99热精品在线 | 99亚洲国产精品 | 久久久久亚洲av毛片大全 | 午夜影院在线 | 日本a∨视频 | 久操av在线 | 欧美综合视频在线 | 天天天久久久 | 精品在线一区二区三区 | av香蕉 | 精品国产乱码久久久久久丨区2区 | 精品欧美一区二区精品久久久 | 国产日韩欧美一区二区 | 国产精品久久久久影院色老大 | www.亚洲一区| 99视频在线免费观看 | 国产精品久久久久久久久久久免费看 | 欧美色人| 精精国产xxxx视频在线野外 | 精品欧美一区二区精品久久 | www.国产一区 | 日本一区二区三区四区 | 久久久日韩精品一区二区三区 | 日韩精品在线视频免费观看 | 久久看片| 欧美xxxx网站 | 精品成人在线观看 | 中文字幕国产视频 | 69精品久久久久久 | 中文字幕在线视频一区二区三区 | 亚洲激情视频在线 | 欧美日韩不卡在线 | 久久综合一区二区三区 | 成人性视频免费网站 | 中文字幕免费视频 | www.伊人.com| 福利久久|