HTML Basics: JS

Source: Internet
Author: User
Tags first string

JS is a scripting language. Play the role of manipulating behavior in HTML. In HTML, if the HTML code is a person, then JS is the person's behavior

JS is referenced in the head of the HTML and can also be referenced in the body. References by <script></script> Tags: <script src= "Public.js" type= "Text/javascript" ></script>

The difference between head and body: Since H ' tml code is parsed, it runs down sequentially from the top. If placed in the head, the Js,js execution will be executed before the HTML in the body will continue to appear on the page. So in this case, we'll open the page and we'll see the long blank space first. Because JS is loading. The user experience is not good. And if you put it in the body. In the execution, the first execution of the body of the HTML code, the page first rendered in front of the user, the last execution to JS, JS can slowly load in the background. Better User Experience

One, string manipulation

//define string var str='you are happy! '; var name=' aaaaaa'; //concatenation of strings var name_str= name+str; //string Manipulation str=' Aaaaajijiji'Str.charat (0) Gets a character in a string based on a corner mark Char character str.substring (1,3gets a string subsequence greater than or equal to x less than Y str.length according to the angle tag gets the string length Str.concat (' first string') Stitching string Str.indexof ('Master') Gets the position of the subsequence str.slice (0,1) slice start end str.tolowercase () change to lowercase str.touppercase () change uppercase Str.split ('Division', 1The slice returns the array parameter 2 for the first X element number type of the array after the split (JavaScript has only one numeric type.) Numbers can be with decimal points or without) Var age= 18; var score= 89.22; number=' -'; //string goto var n=parseint (number);//If number is a decimal point, it will be removed directly//Convert to fractional f=parsefloat (number) Boolean type (TRUE or false) Var T= true; var f = False;//python are both uppercase and lowercase in JS

Second, array

The first method of creating var list =new Array (); List[0]='first element'; list[1] ='a second element'; //The second way of creating Var list2= New Array ('first element','a second element'); //The Third way of creating Var list3= ['first element','a second element']; Array operation list3.length the length of the array List3.push ('DSX'trailing append parameter List3.shift () head Gets an element and removes the element List3.pop () tail gets an element and deletes the element List3.unshift ('DSX'the head inserts a data list3.splice (start, DeleteCount, value) to insert, delete, or replace an array of elements List3.splice (N,0,val) at the specified position to insert the element List3.splice (n,1, Val) specifies the position of the replacement element List3.splice (N,1) to specify the location to delete the element List3.slice (The) slices; List3.reverse () Invert list3.join ('-') to stitch the array into a string List3.concat (['ABC']) array and array stitching list3.sort () sort

Third, the object

A dictionary like in Python

var dict = {name:'dsx', Age:18,sex:' male '== dict['name'];d elete dict['name'] Remove delete dict.age Delete

Iv. circulation

//if (1==2) {//Console.log (1111111)//        }//else if (2==2) {//Console.log (2222)//        }//else{//console.log (' last ')//        }//in JavaScript, if it is two equals, it does not verify that the data type is the same. Three equal signs determine the type of data//if (' 1 ' ==1) {//Console.log (11111)//        }//The above code will print out 11111        if(' 1 ' ===1) {Console.log (1111111)        }        Else{Console.log (222222)        }//The above code will print out 222//Switch        Switch(2){             Case1: Console.log (1111);//in case of multiple lines of code, you must add a semicolon                 Break;//Each branch must be added break             Case2: Console.log (22222);  Break; default: Console.log (33333)        }//For Loop--String        varName= ' Dsx nhy ';  for(varAinchName) {//the circle is the angle mark, 0,1,2,3 ... Console.log (Name[a])}//array, loop, or corner label        varlist=[' big bro ', ' nu ox ', ' ANDAs '];  for(varIinchlist) {Console.log (list[i])}//dictionary, the loop is key        vardict={' BMW ': ' BMW ', ' Mercedes ': ' BC '};  for(varDinchdict)            {Console.log (d); Console.log (Dict[d])}//The following method does not support the rotation of the dictionary        varlist=[' big bro ', ' nu ox ', ' ANDAs '];  for(vari=1;i<=list.length;i++) {console.log (i); Console.log (List[i])}//While Loop         while(1==1) {//dead LoopConsole.log (111111)        }

Five, function

//General Functions        functionF (name,age) {//Can be transmitted or not transmitted.Console.log (name);        Console.log (age); } f (' Dsx ', 19)//self-executing functions(function(name) {Console.log (' Self-executing function ') }) (' Argument name ' )//equivalent to (function ()), the function is enclosed in parentheses into a variable F, and then the variable f is followed by parentheses, which automatically executes F (). Equivalent to the following code//f= (function () {console.log (' FDFD ')});//f ()

Vi. objects

Create an object from the New keyword

In JS, the difference between a method and a class is whether it has this. If the method uses JS, it will automatically resolve to the class

<script>    function  f (name) {        this. Name=name;  the function is automatically treated as a class when it is written in This,js. This is the same as the self in Python        . say= function () {// anonymous functions            Console.log (' say ' +this. Name)        }    }    var obj=New F (' DFDF ' ); // creating an object from New     Console.log (obj.name);    Obj.say ()</script>

HTML Basics: JS

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.