[英]How to override jQuery global functions for my plugin?
我創建了一個插件,該插件可基於隱藏字段創建復選框圖像 。 我想重寫is()
函數...可能嗎? 我基本上是使用圖像來模擬復選框。
HTML:
<input type="hidden" value="1" id="box" />
為隱藏字段創建插件:
$('#box').myCheckbox();
我希望能夠執行以下操作:
$('#box').is(':checked');
是否可以在我的插件中覆蓋is()
函數?
您可以擴展jQuery的選擇器並添加新的選擇器。
http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/
例如:
$.extend($.expr[':'],{
inline: function(a) {
return $(a).css('display') === 'inline';
}
});
這使您可以執行以下操作:
$(':inline'); // Selects ALL inline elements
$('a:inline'); // Selects ALL inline anchors
編輯:
在您的情況下,要模擬此復選框,請首先從jquery:checked ...中獲取代碼:
return elem.checked === true;
然后
$.extend($.expr[':'],{
checked: function(a) {
if(a.isMySpecialCheckbox()) return a.hasAPropertyThatSignifiesChecked;
else return elem.checked === true;
}
});
試試看,我還沒有嘗試過,但是應該可以。
是的:)
(function() {
var proxied = jQuery.fn.is;
jQuery.fn.is= function() {
//do your things here
return proxied.apply(this, arguments);//call the default is here
};
})();
btw :checked
默認情況下已支持:checked
所以如果你有
$('#checkbox')。is(':checked'); //返回true / false,取決於是否勾選了該復選框,不要使事情復雜化,因為稍后當您忘了u時,您會把頭發拉出來進行了此重要更改:)
為什么不能使用.attr('checked')
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.