2014-06-24 29 views
0

我試圖做一個jQuery插件使用引導類。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

只需使用'this'來代替'selector ???????' – Satpal

+0

您的意思是您想要調用'bssuccess'函數的元素的選擇符?如果是這樣,你可以在插件中使用'this'來獲取元素本身。 –

+0

謝謝你的提示。謝謝,蒂爾溫錯誤修復。 – user2301515

回答

2

選擇器作爲this傳遞。

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

jQuery插件使用它來訪問要應用自身的一個或多個元素。