Iv. JavaScript Basics &dom

Source: Internet
Author: User
Tags date1 browser cache

Note Contents Map:

A. JS String object

    • View the W3cschool document, which has a detailed property method for that object.

    • Common Properties
      • Length: The number of characters in the string is declared.
        <script type= "Text/javascript" >        var a = "Drby";    document.write (a.length);    // 4</script>
  • Common methods
    • HTML-related methods
      • Bold (): Use bold to display the string.
        <script type= "Text/javascript" >    var str= "Hello world!"     document.write (Str.bold ())    //helloworld! Show as bold          "find statement end without semicolon can also run successfully qaq"</script>
      • FontColor (): Displays the string using the specified color.
        <script type= "Text/javascript" >    var str= "Hello world!"     document.write (Str.fontcolor ("Red"))</script>
      • FontSize (): Displays a string using the specified dimensions
        <script type= "Text/javascript" >    var str= "Hello world!"     document.write (str.fontsize (7))</script>
      • Link (): Displays the string as a link.
        <script type= "Text/javascript" >    var str= "This is a URL"    document.write (Str.link ( "http://www.w3school.com.cn"))</script>
      • Sub (): Displays the string as subscript. SUP (): Displays the string as superscript.
        <script type= "Text/javascript" >    var str= "Hello world! subscript"    document.write (Str.sub ())     var str1= "Hello world! superscript"    document.write (Str1.sup ())</script>

    • Java-like method ()More examples Usage view documents
      • Concat (): Connection string.
      • CharAt (): Returns the character at the specified position.
      • IndexOf (): Retrieves a string. That is, returns the character position
      • Split (): Splits a string into an array of strings.
      • Replace (): Replaces a substring that matches a regular expression.
      • SUBSTR () substring ():
        <script type= "Text/javascript" >//Concat Method    varSTR1 = "ABC"; varstr2 = "DFG";    document.write (Str1.concat (str2)); document.write ("); //Charat Method    varSTR3 = "ABCDEFG"; document.write (Str3.charat (20));//character position does not exist, returns an empty stringdocument.write (Str3.charat (3)); document.write ("); //IndexOf Method    varSTR4 = "Poiuyt"; document.write (Str4.indexof ("Y")); document.write (Str4.indexof ("W"));//character does not exist, return-1document.write ("); //Split Method "More example Usage view documents"    varSTR5 = "A-b-c-d"; vararr1 = Str5.split ("-"); document.write ("Length:" +arr1.length + "<br/>"); varStr= "How is you doing today?"document.write (Str.split ("") + "<br/>") document.write (Str.split ("") + "<br/>") document.write (Str.split ("", 3)) document.write ("); //Replace Method "More example Usage view documents"    varSTR6 = "ABCD";    document.write (STR6); document.write ("<br/>"); document.write (Str6.replace ("A", "Q")); Pass two parameters, the first original character, and the second is the character to be replaced by document.write ("); //substr Method    varSTR7 = "Abcdefghuiop"; document.write (Str7.substr (5,5));//Fghui starts at fifth place and intercepts severaldocument.write ("); //substring Method    varStr8 = "Abcdefghuiopqrst"; document.write (Str7.substring (3,5));//de from the beginning of the first to the end of the [3,5) package left without packet right</script>

It is important to note that the JavaScript string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string.

Second, JS of the array object

  • Common Properties
    • length: Sets or returns the number of elements in the array.
       <script type= "Text/javascript" > var  arr = new  Array (3) arr[ 0] = "John"  arr[ 1] = "Andy"  arr[ 2] = "Wendy"  document.write ( "Original Length:" + Arr.length) document.write ( "<br/>" ) arr.length  =5 document.write (" New length: "+ Arr.length)  </script>//  result:  // original length:3  // new length:5    
  • Common methods
    • Concat (): Joins two or more arrays and returns the result.
      • The method does not alter the existing array, but only returns a copy of the concatenated array. That is, a new array is returned.
    • Join (): puts all the elements of the array into a string. element is delimited by the specified delimiter.
    • Push (): Adds one or more elements to the end of the array and returns the new length.
      • The push () method adds its parameter order to the tail of the arrayobject. It modifies the arrayobject directly, rather than creating a new array. The push () method and the Pop () method use the advanced post-stack functionality provided by the array.
      • If you add an array, add the array as a whole string at this time.
    • Pop (): Delete and return the last element of the array
      • The Pop () method removes the last element of the arrayobject, reducing the length of the array by 1, and returning the value of the element it deletes. If the array is already empty, pop () does not change the array and returns the undefined value.
    • Reverse (): Reverses the order of the elements in the array.
      • The method changes the original array without creating a new array.
        <script type= "Text/javascript" >//Concat Method    varARR1 = [A]; varARR2 = [4,5,6]; varARR3 = [7,8,9]; document.write (Arr1.concat (ARR2)+ "<br/>");//1,2,3,4,5,6document.write (Arr1.concat (ARR2,ARR3));//1,2,3,4,5,6,7,8,9document.write ("); //Join Method    varARR4 =NewArray (3) arr4[0] = "George"arr4[1] = "John"arr4[2] = "Thomas"document.write (Arr4.join ()+ "<br/>")//George,john,thomasdocument.write (Arr4.join ("."))//George.John.Thomasdocument.write ("); //Push Method    varARR5 =NewArray (3); arr5[0] = "Tom"; arr5[1] = "Lucy"; arr5[2] = "Jack"; document.write ("Old array:" +ARR5); document.write ("<br/>"); document.write ("Old length:" +arr5.length); document.write ("<br/>"); document.write ("New Length:" +arr5.push ("Zhangsan")); document.write ("<br/>"); document.write ("New array:" +ARR5); document.write ("<br/>", "<br/>"); varARR6 = ["www", "QQQ"]; varARR7 = ["AAA", "BBB", "CCC"]; document.write ("Old array:" +ARR6 + "<br/>"); document.write ("Old length:" +arr6.length + "<br/>"); document.write ("New Length:" +arr6.push (ARR7) + "<br/>");//The result 3 is not 5 because the ARR7 is added to the ARR6 as a whole string (that is, an element)document.write ("New array:" +ARR6);  for(vari=0;i<arr6.length;i++) {alert (arr6[i]); } document.write ("); //Pop Method    varARR8 = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu"]; document.write ("Old array:" +ARR8 + "<br/>"); document.write ("Return:" +arr8.pop () + "<br/>"); document.write ("New array:" +arr8); document.write ("); //Reverser Method    varARR9 = ["Zhangsan1", "Lisi1", "zhaoliu1", "Niuqi1"]; document.write ("Old array:" +ARR9); document.write ("<br/>"); document.write ("New array:" +arr9.reverse ());</script>

Third,the date object of JS

  • Get current time in Java
     Date date = new   Date ();  //  tolocalestring () // xxxx year xx month xx day xx:xx:xx  
  • js inside get current time
     <script type= "Text/javascript" > //  get current time  var date = new   Date ();  document.write (date);  //  Sat 22:33:17 gmt+0800 (    Chinese standard Time)  //  Convert to custom format     document.write (" //  May 28, 2016 22:33:17  </ Script> 
  • Gets the current year method getFullYear ()
    New Date ();d ocument.write ("Year:" +date.getfullyear ());  //
  • Gets the current month method GetMonth ()
    <script type= "Text/javascript" >    // return is 0月-November, if you want to get the exact value, add 1    var New Date ();     var date1 = Date.getmonth () +1;    document.write ("Month:" +date1); </script>
  • get current Week  getday ()
     <script type= "Text/javascript" > //  return (0 ~ 6)  //  foreign friends, Sunday as the first day of the week, Sunday returned is 0, and Monday to Saturday returned is 1-6  var  date = new   Date (); document.write ( "Week:" +date.getday ());  </script> 
  • Gets the current day getDate ()
    <script type= "Text/javascript" >    // get current day 1-31           varnew  Date ();    document.write ("Day:" +date.getdate ()); </script>
  • Gets the current hour getHours ()
    <script type= "Text/javascript" >    // get current hour 0~23           var  New  Date ();    document.write ("Hour:" +date.gethours ()); </script>
  • Gets the current minute getminutes ()
    <script type= "Text/javascript" >           varnew  Date ();    document.write ("minute:" +date.getminutes ()); </script>
  • Gets the current seconds getseconds ()
    var New Date ();d ocument.write ("Second:" +date.getseconds ());
  • gets the number of milliseconds getTime () "Returns the number of milliseconds that are 1970 1 1 to date" settime () sets the time based on the number of milliseconds
    var New Date (); document.write ("Second:" +date.gettime ());
    • Application Scenarios:

      Often used to prevent browser cache, such as bank websites do not need to cache

      Use milliseconds to handle cached effects (no cache) http://www.baidu.com? milliseconds

Four, JS Math object

    • The Math object is not a class of objects like Date and String, so there is no constructor for math (), a function like Math.sin () is just a function, not a method of an object. Without creating it, all its properties and methods can be called by using Math as an object.
    • Mathematical arithmetic. All inside are static methods, using the Math directly. Method ()
    • Ceil (x): Rounding up
      <script type= "Text/javascript" >           var mm = 10.3;     // Do not create a math object    document.write (Math.ceil (mm));   // </script>
    • Floor (x): Rounding Down
    • Round (x): Rounding
    • Random (): Gets the stochastic number (pseudo-random number) (returns a random number between 0.0 and 1.0. )
      • Get a random number of 0-9
        Math.random () *10Math.floor (math.random ()*10));      Math.floor to the small number of parts

The global function of the five and JS

Overloading of functions of the six and JS

Iv. JavaScript Basics &dom

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.