JavaScript gets the next node of the DOM book.

Source: Internet
Author: User

Use JavaScript to write an add-minus button on the page to achieve the cumulative number of numbers.

The abbreviated HTML is probably the case. If you can read it, don't mind the details.

1  <inputtype= "button"value="+"onclick= "Jia (this)" />2  <labelclass= "num">0</label>3  <inputtype= "button"value="-"onclick= "Jian (this)" />

It looks like this.

The JavaScript code is as follows

1<script type= "Text/javascript" >2  functionJia (a)3     {4         varNextNode = a.nextelementsibling;//Get Next node5        6 alert (nextnode.innerhtml);7             varA =parseint (nextnode.innerhtml)8A + = 1;9nextnode.innerhtml =A;Ten  One          A     } -     functionJian (a) { -         varPreviousnode =a.previouselementsibling; the         varA =parseint (previousnode.innerhtml) -A-= 1; -A = a > 0? a:0; -previousnode.innerhtml =A; +     } -</script>


function Jian (a) and
function Jia (a) is the object of the current click. Add this to the OnClick event method;

-Nextelementsibling Gets the next node of the current node (obtains the next sibling node)

-Previouselementsibling Gets the previous node of the current node

Note: IE skips the space between nodes (for example: newline characters), and Mozilla does not--ff such a layout element such as a space wrap is treated as a node read, so the next node element can be read in IE with nextsibling, This is what you need to write in FF: nextelementsibling.

The above explanation means using nextelementsibling and previouselementsibling to get the next sibling node and the previous sibling node, you can remove the newline, the space above and so on, directly find our tag element. But the following two

PreviousSibling is also the next sibling node and the previous sibling node, just easy to use in IE
--------------------keyword Explanation
parseint conversion function.
A = a > 0? a:0;----three-dimensional expression. 

JavaScript gets the next node of the DOM book.

Related Article

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: 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.