Jquery Processing strings

Source: Internet
Author: User

1. Remove spaces
var Txt=$.trim ($ ("Txt1"). Val ());

2. Convert to Digital
Txtnum=number ($.trim (TXT)) + 1;
var Thisele = $ ("#para"). CSS ("font-size"); Get font Size
var textfontsize = parsefloat (Thisele, 10);

3. Rounding to integers/random numbers
Math.ceil ()
The Ceil () method rounds up a number.
The parameter must be a numeric value. The return value is greater than or equal to X, and the integer closest to it.
Math.floor ()
The floor () method can be rounded down by a number.
The parameter can be any numeric value or expression. The return value is less than or equal to X and is the nearest integer to X.
Math.Round ()
The round () method rounds a number to the nearest integer
The parameter must be a numeric value. The return value is the nearest integer to X.

Math.ceil (4.8992303) output results: 5
Math.floor (4.8992303) output results: 4
Math.Round (4.8992303) output results: 5
Math.ceil (4.29993354) output results: 5
Math.floor (4.29993354) output results: 4
Math.Round (4.29993354) output results: 4
Math.Round (Math.random () *100); Generate 0-100 of random numbers

4. Intercepting strings
var txt=$ ("P"). Text (). substr (0,15);//intercept 15 characters starting from the first character

5. String substitution
$ ("image"). attr ("src"). Replace ("size=60", "size=200"); Usage replace (target to replace, new value replaced)
Match a regular replacement such as: $ ("#txt"). Replace (/[^\d-]/g, ""). Replace (/^\-/g, "");

6. Splitting a string
var str=new String ();
var arr=new Array ();
Str= "Baidu, farmer it station, Google, Bamboo Wind, nongfuit.com, Web Exchange Group, 180550045 Welcome to join";
Arr=str.split (', ');//note split can be separated by character or string
Alert (Str.split (', ') [1]);
for (Var i=0;i<arr.length;i++)
{
Alter (Arr[i]);
}

7.js and jquery objects convert to each other
var AA = $ ("#mm"). Get (0); jquery object into JS object
var bb = $ (AA); JS object into jquery object

8. Using regular matching
var Matchtel =/^ (([0\+]\d{2,3}-)? ( 0\d{2,3})-) (\d{7,8}) (-(\d{3,}))? $/;
if (!matchtel.test ($ ("#txtTel"). Val ())) {
Alert ("Phone format is wrong! ");
Return! 1;
}

Jquery Processing strings

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.