Javascrip--dom action (Find HTML element/modify Element)

Source: Internet
Author: User

InnerHTML

1. Find element--document.getelementbyid ("Intro")

2. Results of output Lookup:

(1) var A=document.getelementbyid ("Intro");

document.write (a.innerhtml);

(2) <div id= "Me" ><b> try it </b></div>

var B=document.getelementbyid ("Me");
document.write (b.innerhtml); Write output try it.

Alert ( b.innerhtml)//alert output is <b> try it </b> code + text

Alert (b.innertext)//Output Text only-try it

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">//find elements in HTML by IDvarA=document.getelementbyid ("Intro");d ocument.write (a.innerhtml);varB=document.getelementbyid ("Me");d ocument.write (b.innerhtml);//write output try it.Alert (b.innerhtml)//alert output is <b> try it </b> code + textAlert (B.innertext)//output Text Onlyb.innerhtml=" ";//assign an empty stringB.innerhtml= "<font color=red> I changed </font>";//re-set content//can be written directly/*var B=document.getelementbyid ("Me");d ocument.write (b.innerhtml= "I Changed")*/</script>

The result is:

Example: Using the ID of the P tag to get the content and modify it to the new content

<! DOCTYPE html> document.getElementById ("P1"). Innerhtml= "New text!"; </script><p> the above paragraph was modified by a JavaScript script. </p></body>

Results:

New text!

The above paragraph was modified by a JavaScript script.

Another example:

The same way, just a different notation.

<! DOCTYPE html>var element= document.getElementById ("header"); element.innerhtml= "New header"; </script><p> "Old Header" has been modified to "New header". </p></body>
New Header

"Old Header" has been modified to "New header".

To give another, change the picture:

<! DOCTYPE html> document.getElementById ("image"). src= "/i/shanghai_lupu_bridge.jpg"; </script><p> Original picture is Tulip (eg_tulip.jpg), but has been modified to Lupu Bridge (shanghai_lupu_bridge.jpg). </p></body>

The original picture is Tulip (eg_tulip.jpg), but has been modified to Lupu Bridge (shanghai_lupu_bridge.jpg).

Javascrip--dom action (Find HTML element/modify Element)

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.