Ie7/8 error: the object does not support the "trim" attribute or method.

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.