Home > Web Front-end > JS Tutorial > JavaScript basic introductory syntax collection_basic knowledge

JavaScript basic introductory syntax collection_basic knowledge

PHP中文网
Release: 2016-05-16 19:01:00
Original
761 people have browsed it

For novices, if you master the following methods, you can basically make some js yourself, which is a must for getting started

Create script block

Hide script code



Browse



Link to external script file



Comment script

// This is a comment
document .write(“Hello”); // This is a comment
/*
All of this
is a comment
*/

Output to browser

document.write(“Output content”);

Define variables

var myVariable = “some value”;

characters String addition

var myString = “String1” “String2”;

String search



String replacement

thisVar.replace(“Monday”,”Friday”);

Format string

< ;script language="JavaScript">



Create array



Array sorting



Split string



Pop up warning message



Pop-up confirmation box



Call JS function

< ;a href="#" onClick="functionName()">Link text
Link text

Execute the function after the page is loaded


Body of the page


Conditional judgment

<script> <br><!-- <br/>var userChoice = window.confirm(“Choose OK or Cancel”); <br/>var result = (userChoice == true) ? “OK” : “Cancel”; <br/>document.write(result); <br/>// --> <br></script>

指定次数循环

<script> <br><!-- <br>var myArray = new Array(3); <br>myArray[0] = “Item 0”; <br>myArray[1] = “Item 1”; <br>myArray[2] = “Item 2”; <br>for (i = 0; i < myArray.length; i ) { <br>document.write(myArray “<br>”); <br>} <br>// --> <br></script>

设定将来执行

<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>} <br/>window.setTimeout(“hello()”,5000); <br/>// --> <br></script>

定时执行函数

<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>window.setTimeout(“hello()”,5000); <br/>} <br/>window.setTimeout(“hello()”,5000); <br/>// --> <br></script>

取消定时执行

<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>} <br/>var myTimeout = window.setTimeout(“hello()”,5000); <br/>window.clearTimeout(myTimeout); <br/>// --> <br></script>

在页面卸载时候执行函数

Body of the page

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template