JavaScript DOM Programming Art reading notes JavaScript picture library _javascript Tips

Source: Internet
Author: User

Two ways to change the src attribute of a picture:

The 1,setattribute method is part of the "level 1th dom", which can set any property of an element node.

2,ELEMENT.SRC = Source; This is the way before "level 1th dom" appears, and it is now valid.

The advantage of "level 1th dom" is portability, and those old methods apply only to Web documents, and DOM applies to any markup language.

Event handler function

When I click on a link, I want to stay on this page rather than go to another window, the code is as follows:

Copy Code code as follows:

<a href= "http://www.example.com" onclick= "Showpic" (this); return false; >Click</a>

When this link is clicked, the default behavior of this link is not triggered because the JavaScript code that is triggered by the OnClick event handler returns a false value.

ChildNodes Property

The ChildNodes property can be used to get all the child elements of any one element. The array returned by the ChildNodes property contains all types of nodes, not just element nodes. In fact, almost everything in the document is a node, and even spaces and line breaks are interpreted as nodes, all of which are contained in the array returned by the ChildNodes property.

However, each node has a NodeType attribute. The NodeType attribute has a total of 12 desirable values, but only 3 of them have value: the NodeType property value of the element node is 1, the NodeType property value of the attribute node is 2, and the NodeType property value of the text node is 3.

If you want to change the value of a text node, use the NodeValue attribute provided by the DOM.

Array element Childnodes[0] has a more intuitive and readable synonym that can be written as Firstchild;dom and provides a corresponding LastChild attribute.

Example:

Copy Code code as follows:

<p id= "description" >choose an image</p>

First create a variable to store it:

Copy Code code as follows:

var description = document.getElementById ("description");

The return value of the description.nodevalue is null. The NodeValue property of the <p> element itself is a null value, and if you want to get the value of the text contained in the <p> element, use Description.childnodes[0]. NodeValue or Description.firstChild.nodeValue.

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.