Javascript learning notes (1) Basic DOM operations

Source: Internet
Author: User
The main purpose is to make yourself more proficient in DOM operations, record your own bit by bit, and standardize your own code! Hope everyone can make progress together! Partial html code: When you click show, The showValue function is triggered to dynamically Add the inputvalue value to the idtext element node! Viewsourceprint? & Lt; p & gt; & nbsp; S

The main purpose is to make yourself more proficient in DOM operations, record your own bit by bit, and standardize your own code! Hope everyone can make progress together!
Some html code:
When you click show, The showValue function is triggered to dynamically Add the value of input value to the element node id = "text!
View sourceprint?

  

  

Javascript code:
When a page is loaded, the input automatically obtains the focus and directs the user to input the content. (Details enhance user experience)
View sourceprint? 1 window. onload = function (){

2 var user_name = document. getElementById ("user_name ");

3 user_name.focus ();

4}

If no content is entered, an error message is displayed, and the input focus is obtained!
View sourceprint? Function showValue (){

Var user_name = document. getElementById ("user_name ");

Var text = document. getElementById ("text ");

If (user_name.value = ""){

Alert ("Please input some words ");

User_name.focus ();

} Else {

Text. innerHTML = user_name.value;

Text. setAttribute ("class", "text ");

}

}

HTML code is completely unnecessary. We can use DOM to dynamically create and add it to the document!
View sourceprint? // Create a div Element

Var text = document. createElement ("div ");

// Add the value of user_name.value to the div element.

Text. appendChild (user_name.value );

// Add the div element to the body.

Document. body. appendChild (text );

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.