When the trim attribute in jquery is used today,: $ ('input [type = \ 'text \ ']: eq (0 )'). val (). trim (), error in ie7/8: The object does not support the "trim" attribute or method. Next I will introduce the solution for you.
Solution:
Method 1:
Use the global functions in jquery $. trim () replaces the native js method trim (): $. trim ($ ("input [type = 'text']: eq (0 )"). val (). trim ());
Method 2:
Use native js to extend the String method
1. Method for writing classes: [Call format: str. trim ();]
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript">
Function. prototype. method = function (name, func ){
This. prototype [name] = func;
Return this;
};
If (String. prototype. trim) {// determines whether the browser has a trim () method.
String. method ('trim', function (){
Return this. replace (/^ s + | s + $/g ,'');
});
String. method ('ltrim', function (){
Return this. replace (/^ s +/g ,'');
});
String. method ('rtrim ', function (){
Return this. replace (/s + $/g ,'');
});
}
</Script> |
Example
| The Code is as follows: |
Copy code |
|
<Input id = "demo" type = "" value = "Left and Right spaces"/> <a href = "javascript:;" onclick = "test (); "> click to remove spaces </a> <script type =" text/javascript ">
Function. prototype. method = function (name, func ){
This. prototype [name] = func;
Return this;
};
If (! String. prototype. trim) {// determines whether the browser has a trim () method.
String. method ('trim', function (){
Return this. replace (/^ s + | s + $/g ,'');
});
String. method ('ltrim', function (){
Return this. replace (/^ s +/g ,'');
});
String. method ('rtrim ', function (){
Return this. replace (/s + $/g ,'');
});
}
// Test the call method: trim ()
Document. getElementById ("demo"). select ();
Var str = document. getElementById ("demo"). value;
Function test (){
Document. getElementById ("demo"). value = str. trim (); // you can change it to str. ltrim () or str. rtrim ()
Document. getElementById ("demo"). select ();
}
</Script> |
Solution 2
Written as a function: [Call format: trim (str)]
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript">
Function trim (str) {// Delete spaces between the left and right sides
Return str. replace (/(^ s *) | (s * $)/g ,"");
}
Function ltrim (str) {// Delete the left space
Return str. replace (/(^ s *)/g ,"");
}
Function rtrim (str) {// Delete the right space
Return str. replace (/(s * $)/g ,"");
}
</Script> |
Instance
| The Code is as follows: |
Copy code |
|
<Input id = "demo" type = "" value = "Left and Right spaces"/> <a href = "javascript:;" onclick = "test (); "> click to remove spaces </a> <script type =" text/javascript ">
Function trim (str) {// Delete spaces between the left and right sides
Return str. replace (/(^ s *) | (s * $)/g ,"");
}
Function ltrim (str) {// Delete the left space
Return str. replace (/(^ s *)/g ,"");
}
Function rtrim (str) {// Delete the right space
Return str. replace (/(s * $)/g ,"");
}
// Test the call method: trim ()
Document. getElementById ("demo"). select ();
Var str = document. getElementById ("demo"). value;
Function test (){
Document. getElementById ("demo"). value = trim (str); // you can change it to str. ltrim () or str. rtrim ()
Document. getElementById ("demo"). select ();
}
</Script> |