The solution that javascript trim functions cannot be used in IE is javascripttrim.

Source: Internet
Author: User

The solution that javascript trim functions cannot be used in IE is javascripttrim.

Javascript trim functions are available in firefox.

<script language="javascript"> var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim(); </script>

There is no problem in using Firefox, but an error is reported in IE.
So we can modify it.

String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");}

Add this sentence in the header, and the above statement can be run in both IE and FF.

<script language="javascript"> String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");} var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim(); </script>

JQuery provides the following methods:

<!DOCTYPE html> 

The trim () function in JQuery is supported in firefox, but not in IE. How can I make IE9 compatible?

$. Each (ob, function (k, val ){
If (k = 'cname '){
Var s = $. trim (ob. cname );
$ ("# Nianji "). append ("<option value = '" + s + "'>" + ob. cname. trim () + "</option> ");
}
});

This is all supported. $. trim ()

Which of the following experts added the trim () function to javascript gave me some advice: Why can't I verify the Code as follows?

<Form id = "form1" name = "form1" method = "post" action = "" onsubmit = "return yz ()">
<Input type = "text" name = "text1"/>
<Input type = "submit" name = "Submit" value = "submit"/>
</Form>
<Script language = javascript>
String. prototype. Trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Function yz ()
{
If (document. form1.text1. value. Trim () = "")
{
Alert ("null ");
Return false;
}
}
</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.