JavaScript stat...LOGIN

JavaScript statements

JavaScript statement sends a command to the browser. The purpose of a statement is to tell the browser what to do.

document.getElementById uses

Syntax: oElement = document .getElementById (sID)

Parameters: sID--required options. String.

Return value: oElemen--Object (Element).

Description: Get the object based on the specified id attribute value. Returns a reference to the first object whose id attribute value is equal to sID. If the corresponding object is a group of objects, the first object in the group is returned. If there is no matching object, return null .

Note: document.getElementById(" ") gets an object. Use alert to display "object" instead of a specific value. It has attributes such as value and length. Add .value to get is the specific value!

Details:

① document.getElementById sometimes captures name and lets go of id. It is said to be a BUG of IE

② GetElementbyId in javascript is used
in the web page The element must have an id attribute before it can be obtained through this method, such as <input type=textname="content" id="content">

③There are two main ways to obtain html tags, one is Through the ID value, one is through the name attribute (the name attribute is mainly used for the input tag in the form.)

Note:

document.getElementById(" " ) What you get is an object. Use alert to display the

"object" instead of a specific value. It has attributes such as value and length. Add .value to get the

It’s a specific value!

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
    <p id="demo"></p>
    <script>
    document.getElementById("demo").innerHTML = "你好 summer";
    </script>
</body>
</html>

Semicolon ;

Semicolon is used to separate JavaScript statements.

Usually we add a semicolon at the end of each executable statement.

Another use of semicolons is to write multiple statements on one line.

a = 5;
b = 6;
c = a + b;

JavaScript Code

JavaScript 代码是 JavaScript 语句的序列。
浏览器按照编写顺序依次执行每条语句。
本例向网页输出一个标题和两个段落:
<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
    <p id="demo">一句问候</p>
    <div id="myDIV">一段话</div>
    <script>
    document.getElementById("demo").innerHTML="你好 summer";
    document.getElementById("myDIV").innerHTML="你最近过得怎么样?";
    </script>
</body>
</html>

JavaScript Code Blocks

JavaScript can be combined in batches.

A code block starts with a left curly brace and ends with a right curly brace.

The function of a code block is to execute a sequence of statements together.

This example outputs a title and two paragraphs to the web page:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script>
    function myFunction(){
    document.getElementById("myPar").innerHTML="你好,世界!";
    document.getElementById("myDiv").innerHTML="这里要显示一句话。";
    }
    </script>
</head>
<body>
    <p id="myPar">替代</p>
    <div id="myDiv">覆盖</div>
    <p>
    <button type="button" onclick="myFunction()">点击这里</button>
    </p>
</body>
</html>

JavaScript statement identifier

JavaScript statements usually start with a statement The identifier starts with and executes the statement.

Statement identifiers are reserved keywords and cannot be used as variable names.

The following table lists JavaScript statement identifiers (keywords):

<table class="reference" "style="width: 100%" style="border: 0px; margin: 4px 0px; padding: 0px; width: 729px; color: rgb(51, 51, 51); font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, STHeiti, "Microsoft Yahei", sans -serif; font-size: 12px; line-height: normal; white-space: normal; widows: 1; background-color: rgb(255, 255, 255);">

Statement Description

##break is used to break out of the loop.

catch statement block, execute the catch statement block when an error occurs during the try statement block. .

continue Skip an iteration in the loop.

do... while Execute a statement block and continue executing the statement block when the conditional statement is true. for When the conditional statement is true, you can execute the code block a specified number of times.

for ... in is used to traverse the properties of the array or object (loop through the properties of the array or object).

#function Define a function

if ... else Used to perform different actions based on different conditions.

return Exit function

switch Used based on Different conditions perform different actions.

throw Throws (generates) an error.

try Implements error handling and is used with catch.

var declares a variable.

while When the conditional statement is true, execute the statement block

##

JavaScript is case sensitive.

JavaScript is case-sensitive.

When writing JavaScript statements, please pay attention to whether the case switching key is turned off.

The functions getElementById and getElementbyID are different.

Similarly, variables myVariable and MyVariable are also different.


Spaces

JavaScript will ignore extra spaces. You can add spaces to your script to make it more readable. The following two lines of code are equivalent:

var person="Hege";
var person = "Hege";


Wrap lines of code

You can wrap lines of code using backslashes in text strings. The following example will display correctly:

document.write("Hello\
world!");

However, you cannot wrap lines like this :

document.write \
("Hello world!");



##Next Section

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> function getAge(){ var age; age = document.getElementById("age_input").value; if ( age == "" ) { alert("请输入您的年龄!"); return false; } if ( age > 25 ) { alert("您的岁数大于 25 岁。"); } else if ( age < 25 ) { alert("您的岁数小于 25 岁。"); } else { alert("您的岁数等于 25 岁。"); } } </script> </head> <body> 您的年龄:<input type="text" id="age_input" /> <input type="button" onclick="getAge()" value="确定" /> </body> </html>
submitReset Code
ChapterCourseware