A summary of common methods of JavaScript

Source: Internet
Author: User
Tags button type

1. JavaScript: Write HTML output

The

code is as follows:


document.write ("<h1>this is a heading</h1>");


document.write ("<p>this is a paragraph</p>");

2. JavaScript: Responding to events

The

code is as follows:


<button type= "button" onclick= "alert (' welcome! ')" > Click here </button>

3. JavaScript: Change HTML content

The

code is as follows:


X=document.getelementbyid ("demo")//Find element


x.innerhtml= "Hello JavaScript"; Change Content

4. JavaScript: changing HTML images

The

code is as follows:


Element=document.getelementbyid (' myimage ')


element.src= ". /i/eg_bulboff.gif ";

5. Change HTML Style

The

code is as follows:


X=document.getelementbyid ("demo")//Find element


x.style.color= "#ff0000"; Change Style

6, JavaScript is sensitive to case.

JavaScript is sensitive to capitalization.
When writing JavaScript statements, be aware of closing the case toggle key.
The function getElementById is different from the getElementById.
Similarly, variable myvariable and myvariable are also different.

7, Hint: a good programming habit is, at the beginning of the code, unified to the required variables to declare.

8, Value = undefined

In computer programs, variables that are not valued are often declared. A variable that is not declared with a value, and its value is actually undefined. After the following statements have been executed, the value of the variable carname will be undefined:
var carname;

9. Create JavaScript objects
This example creates an object named "Person" and adds four properties to it:

copy code code as follows:


person=new Object ();


person.firstname= "Bill";


person.lastname= "Gates";


person.age=56;


person.eyecolor= "Blue";

10. JavaScript Form Verification

Required (or required) items
The following function is used to check whether the user has filled in the required (or required) items in the form. If a required or required option is blank, the warning box pops up and the function's return value is false, otherwise the function's return value is true (meaning there is no problem with the data):

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

The above mentioned is the entire content of this article, I hope you can enjoy

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.