How to apply JQuery function to all elements except some elements?
I am kind of stuck on this one. I have webapp where I use simple JQuery plugin:
$.extend($.fn.disableTextSelect = function() {
return this.each(fun开发者_如何学Pythonction(){
if($.browser.mozilla){//Firefox
$(this).css('MozUserSelect','none');
}else if($.browser.msie){//IE
$(this).bind('selectstart',function(){return false;});
}else if ($.browser.webkit) { // chrome, webkit
$(this).css('webkitUserSelect','none');
this.onselectstart = function () { return false; };
}else{//Opera, etc.
$(this).mousedown(function(){return false;});
}
});
});
This plugin generally disables mouse text selects on all the browsers. I call it on #desktop element, which is basically main wrapper:
$('#desktop').disableTextSelect();
I use this, because it disables mouse text selects on all the elements inside #desktop. However, there are some of elements in #desktop, which I want to have normal text select behaviour. Is there some easy way how to implement code, that would make exception to the global rule ?
JQuery supports the not()
selector (here). You could use the distinguishing feature of those elements you wish to have normal behaviour, as the argument to the not()
selector
The easiest way to do this would be do do some filtering before you start your main logic.
$.extend($.fn.disableTextSelect = function() {
return this.each(function(){
if ($(this).parents('#excludedItems').length) { //or any other logic here
return true; //move to the next item
}
if($.browser.mozilla){//Firefox
$(this).css('MozUserSelect','none');
}else if($.browser.msie){//IE
$(this).bind('selectstart',function(){return false;});
}else if ($.browser.webkit) { // chrome, webkit
$(this).css('webkitUserSelect','none');
this.onselectstart = function () { return false; };
}else{//Opera, etc.
$(this).mousedown(function(){return false;});
}
});
});
精彩评论