JavaScript Harness Web page-Get _javascript tips for page elements

Source: Internet
Author: User

Recommended reading: JavaScript Harness Web-dom

JavaScript Harness Web-CSS and Dom

Segmenting HTML with DOM

Using JavaScript to control Web content is really like cooking. Just don't have to pack up the leftovers, but there is no way to enjoy the delicious fruit.

However, you will be able to complete the HTML elements of the Web content: More importantly, you will have the ability to replace the page components.

JavaScript allows you to manipulate the HTML code of a Web page at will, opening all sorts of interesting doors of opportunity, all because of standard objects: the existence of DOM

getElementById

HTML tags have an "id" attribute, which is unique to the label, and
can be obtained by the id attribute
<body>
<div id= "Div1" >
<div id= " Div2 ">
content
</div>
</div>
</body>
var scenedesc= document.getElementById ("Div2");
getElementById can access the value of the ID in the tag bracket through the element's ID attribute

getElementsByTagName

You can also use the tag name to get elements
<body>
<div id= "Div1" >
<div id= "Div2" >
<div id=
"Div3" > Content
</div>
</div>
</div>
</body>
var divs= document.getElementsByTagName ("div");
getElementsByTagName returns all div tags, the result is an array, and the results are listed in parentheses in the order of the tags in html the
tag name
var divs= document.getElementsByTagName ("div") [2];
Get a third div tag with an index

InnerHTML

The innerHTML feature provides access to all content stored in the element
through innerHTML access to the content stored within the element:
<div id= "Div1" >
<p id= "Story" > You are are standing</p> <strong>alone</strong> in the
Woods.
</div>
</body>
document.getElementById ("story"). InnerHTML;
The return content is: You are standing alone in the woods.
InnerHTML gets all the content and tags under the specified element
InnerHTML can also be used to set Web page content
document.getElementById ("story"). Innerhtml= "You are <strong>not</strong> alone! ";
innerHTML is used only to set labels that can contain text

About JavaScript Control Web page-Get the knowledge of the page elements to introduce so much, I hope to help you!

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.