Using JS to get the node

Source: Internet
Author: User

The HTML code is like this

<div> <p> Owen Perry's works always have a nostalgic cinematic feel. The web designer has been involved in photography for three years. On circa 1983, Owen shows the photos he has taken from his travels in Canada and abroad since the beginning of 2011.                His work is like a retro postcard, like a painting painted on kraft paper a long time ago. </p> <textarea class= "text" ></textarea> <button id= "sub" class= "but" ; reply </button> </div>



Originally feel that click the Reply button get <p></p> inside innerHTML use

function Onclickbut () {var = document.getelementsbyclassname ("but"), for (var i = 0;l=but.length,i<l;i++) {But[i]. onclick = function () {var node = this.parentnode.previoussbiling.previoussbiling.innerhtml;//Gets the contents of the P node}}}

But I don't know what's wrong.

Later felt that the use of

function Onclickbut () {var = document.getelementsbyclassname ("but"), for (var i = 0;l=but.length,i<l;i++) {But[i]. onclick = function () {var node = this.parentnode.childnodes.firstchild.innerhtml;//Gets the contents of the P node}}}

But they can't get it.

Then simply directly

function Onclickbut () {var = document.getelementsbyclassname ("but"), for (var i = 0;l=but.length,i<l;i++) {But[i]. onclick = function () {var node = this.parentNode.getElementsByTagName ("P") [0].innerhtml;//gets the contents of the P node}}}


Ask Daniel for a more convenient way!!


Using JS to get the node

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.