我试图使用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);
如何在函数定义中读取选择器?
答案 0 :(得分:2)
选择器以this
传递。
$.fn.bssuccess = function(condition, hdr, msg) {
//How to read a selector????
this.bsalert("alert alert-success", condition, hdr, msg);
};
jQuery插件使用它来访问一个或多个要自我应用的元素。