jquery并使用预定义的函数

时间:2014-06-24 07:58:42

标签: jquery

我试图使用bootstrap类创建一个jquery插件。

(function($) {
    $.fn.bsalert = function(klass, condition, hdr, msg) {
        if (condition) {
            this.html('<strong>' + hdr + '!</strong> ' + msg);
            this.addClass(klass);
        } else {
            this.html("");
        }
    };
    $.fn.bssuccess = function(condition, hdr, msg) {
        //How to read a selector????
        $(selector???????).bsalert("alert alert-success", condition, hdr, msg);
    };
    $.fn.bswarning = function(condition, hdr, msg) {
        $(selector???????).bsalert("alert alert-warning", condition, hdr, msg);
    };
})(jQuery);

我尝试使用它:

$("#successmsg").bssuccess(error==="","OK","you've done");
$("#errormsg").bswarning(error!=="","Error",error);

如何在函数定义中读取选择器?

1 个答案:

答案 0 :(得分:2)

选择器以this传递。

$.fn.bssuccess = function(condition, hdr, msg) {
    //How to read a selector????
    this.bsalert("alert alert-success", condition, hdr, msg);
};

jQuery插件使用它来访问一个或多个要自我应用的元素。