簡體   English   中英

jQuery插件通過函數返回值

[英]jquery plugin pass value by function return

我有一個簡單的jquery插件。 我傳遞值時工作正常。 但是當函數返回值時不起作用。 它顯示了功能定義。

請在Jsfiddle中嘗試

(function ( $ ) {
    $.fn.greenify = function( options ) {

        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white",
            selectedID:function(){}    
        }, options );

        alert(settings.selectedID);
    };
}( jQuery ));


//Plugin Call
$( "div" ).greenify({
    color: "orange",
    //selectedID:6  it is working fine
    selectedID:function (){return 5}
});

當您調用alert(settings.selectedID) ,您正在傳遞一個對alert()的函數引用,相反,您需要調用settings.selectedID引用的函數,並將其返回的值傳遞給alert()

所以嘗試

alert(settings.selectedID());

演示: 小提琴

您在這里缺少"()"

alert(settings.selectedID);

因此,理想情況下,您必須調用該函數才能正常工作

alert(settings.selectedID());

settings.selectedID是一個函數

alert(settings.selectedID());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM