Well, I've always tended to avoid using blind regexes to perform class  
additions and removals and opted for an array based solution instead.  
Here's my (untested) addClass, removeClass solution as it would appear  
replacing the current system:

jQuery.extend({
  className: {
   add: function(o,c){
    if (jQuery.className.has(o,c)) return;
    var classNames = o.className.replace(/^\s+|\s+$/g,'').split(/\s+/);
    classNames.push(c);
    return o.className = classNames.join(' ');
   },
   remove: function(o,c){
    if (jQuery.className.has(o,c)) {
     var result = [];
     var classNames = o.className.replace(/^\s+|\s+$/g,'').split(/\s+/);
     for (var i = 0; i < classNames.length; ++i)
      if (classNames[i] != c)
       result.push(classNames[i]);
     o.className = result.join(' ');
    }
    return !o.className ? false : o.className;
   },
   has: function(e,a) {
    if ( e.className != undefined )
     e = e.className;
    return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e);
   }
  }
});

I've never had problems with unwanted spaces before or after  
classNames with this code but it's probably heavier than the simple  
regexes in the current system.

-blair

Quoting Matthew Delmarter <[EMAIL PROTECTED]>:

> Hi John,
>
> I just noticed the removeClass problem in IE tonight as well. This code was
> not consistently working:
>
> $("#element").find("table").removeClass("active");
>
> After looking back in the mailing list I came upon this description of the
> problem:
>
> "Hi there, I found this little bug in jQuery that prevents removeClass from
> working when there is a space just before the name of the class. Eg it will
> not work on this element: <div class=" myClass1">
>
> The funny thing is that jQuery is the culprit creating the space: It is left
> there when you apply removeClass to the first of two or more classes on an
> element."
>
> I didn't exactly understand the description 100%, but as a result I tried
> running the following code:
>
>       $("#element").find("table").removeClass("active");
>       $("#element").find("table").removeClass(" active");
>
> This behaves perfectly in IE.
>
> I hope this may help narrow down the problem and provide others with a
> short-term solution...
>
> Regards,
>
> Matthew Delmarter
>
>
>> -----Original Message-----
>> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
>> Behalf Of Andrew Buzzell
>> Sent: Sunday, 3 September 2006 1:33 a.m.
>> To: discuss@jquery.com
>> Subject: Re: [jQuery] Bugs in 1 and 1.01
>>
>>
>> Great! I could have sworn some of the docs and visualjquery (nice work
>> Yehuda) still had set yesterday, but I'm seeing attr now.
>>
>> When I'm debugging jquery problems, I have a habit of switching between
>> three or four different versions I have kicking around, and I know one
>> of them did work with toggle/remove on multiple classes. I'll see if I
>> can find it.
>>
>>
>>
>>
>>
>> John Resig wrote:
>> >> It seems that set (both name:value and hash) is not working, as well as
>> >> removeclass on multiple classes (and, of course, toggleclass with
>> >> multiple classes).
>> >
>> > In 1.0, .set is now .attr - I made this particular change as it was
>> > much clearer, and now affords you with the ability to do:
>> >
>> > $("div").attr("height")
>> >
>> > to get the height of the first element matched (for example)
>> >
>> > I'm not sure if removeClass and toggleClass ever worked on multiple
>> > classes - but if they did, cool :-) I'll have to see if I can get
>> > those features back in.
>> >
>> > --John
>> >
>>
>>
>> _______________________________________________
>> jQuery mailing list
>> discuss@jquery.com
>> http://jquery.com/discuss/
>
>
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to