[jQuery] toggleClass and removeClass
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 10 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Monotype Corsiva";
panose-1:3 1 1 1 1 2 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoAutoSig, li.MsoAutoSig, div.MsoAutoSig
{margin:0in;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman";}
span.EmailStyle17
{font-family:Arial;
color:windowtext;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I have found a couple of fixes for the problem of using
toggleClass and removeClass in Internet Explorer (IE). It seems the root
of the problem is that when you have multiple classes assigned to the className
property of an element in IE (“class1 class2”) and you remove one
of the classes from the className property, you can be left with a className
property containing classes with leading spaces (“ class2”).
What is even worse, if you remove both classes multiple times, you can end up
with the className property containing just blank spaces
(“ “). It is these leading spaces which are
causing toggleClass and removeClass to fail with multiple classes assigned to
the className property. This problem does not seem to exist in Firefox
since it seems to remove leading and trailing spaces when the className
property is changed.</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>After doing some research, I have come up with two possible
solutions. The first solution involves modifying the regular expression
used to remove the class from the className property. On line 345 of the
jQuery source code, you will find the following:</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'>new RegExp("(^<b><span style='font-weight:bold'>|</span></b>\\s*\\b[^-])"+c+"($<b><span
style='font-weight:bold'>|</span></b>\\b(?=[^-]))", "g"),
"") ); </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Change the regular expression to the following:</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'>new RegExp("(^\\s*\\b[^-]<b><span
style='font-weight:bold'>|</span></b>)"+c+"($\\b(?=[^-])<b><span
style='font-weight:bold'>|</span></b>)", "g"), "") ); </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Note the position of the pipe (</span></font><b><font
size=2 face="Courier New"><span style='font-size:10.0pt;font-family:"Courier New";
font-weight:bold'>|</span></font></b><font size=2 face=Arial><span
style='font-size:10.0pt;font-family:Arial'>) characters. This solution
will enable the removal of the specified class from the className property in
IE, however, it does not solve the problem of the extra spaces. Although
the class being toggled or removed can now be found within the className
property, the className property will continue to grow with extraneous blank
spaces each time a class is removed. The blank spaces do not seem to
cause any short term problem, however, they might if the user uses the Web application
for more than a short period.</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>The other solution is more of a sledgehammer approach as it
involves removing the extraneous blank spaces from the className property
anytime a class is removed. This solution involves trimming the spaces
from the className during the class removal. On lines 343 to 345 of the
jQuery source code you will see the following implementation of the
“remove” method of the className property:</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'>o.className = !c ? "" :</span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'> o.className.replace(</span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'> new
RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))",
"g"), "");</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>If you change this code to use the “trim” method
of the jQuery class, you will have the following:</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'>o.className = jQuery.trim( !c ? "" :</span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'> o.className.replace(</span></font>
<p class=MsoNormal><font size=2 face="Courier New"><span style='font-size:10.0pt;
font-family:"Courier New"'> new
RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"),
"") );</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>This solution does not require changing the regular
expression since the extraneous spaces will no longer appear within the
className property. This is the solution I am currently using, however,
if someone has a better solution, please let me know.</span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoAutoSig><b><font size=3 face="Monotype Corsiva"><span
style='font-size:12.0pt;font-family:"Monotype Corsiva";font-weight:bold'>Mark
D. Lincoln</span></font></b>
<p class=MsoAutoSig><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> </span></font>
<p class=MsoAutoSig><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Mark D. Lincoln, Director of Research & Development</span></font>
<p class=MsoAutoSig><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Eye On Solutions, LLC</span></font>
<p class=MsoAutoSig><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>(866) 253-9366x101</span></font>
<p class=MsoAutoSig><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><a href="http://www.eyeonsolutions.com">www.eyeonsolutions.com</a></span></font>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> </span></font>
</div>
</body>
</html>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/