Examples of innerText and innerHTML attribute usage in javascript

Source: Internet
Author: User

Examples of innerText and innerHTML attribute usage in javascript

This article describes how to use innerText and innerHTML attributes in javascript. Share it with you for your reference. The specific analysis is as follows:

Almost all DOM elements have the innerText and innertHTML attributes (case sensitive ).

And the HTML source code. These two attributes are readable and writable.

InnerHTML can also replace createElement, which is simple, extensive, and created at your own risk.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>

<Title> test (innerText and innerHTML) </title>

<Script type = "text/javascript">

Function TestOutput (){

Var myLink = document. getElementById ("lnk ");

Alert ('nertext --> '+ myLink. innerText );

// Obtain the Baidu website

Alert ('nerhtml --> '+ myLink. innerHTML );

// Get a hundred <font color = "red"> website </font>

}

Function EditInnerText (){

Var myLink = document. getElementById ("lnk ");

MyLink. innerText = "phoenixnet ";

}

Function EditInnerHTML (){

Var myLink = document. getElementById ("lnk ");

MyLink. innerHTML = "<font color = 'blue'> Phoenix </font> ";

}

Function dynamicButtonClick (){

Alert ('I am a dynamic create ');

}

Function CreateButton (){

Var div = document. getElementById ("divMain ");

Div. innerHTML = "<input type = 'button 'value = 'click me'

Onclick = 'dynamicbuttonclick () '/> ";

}

</Script>

</Head>

<Body>

<A href = "http://www.baidu.com" id = "lnk">

Hundreds of <font color = "red"> websites </font> </a>

<Input type = "button" value = "test" onclick = "TestOutput ()"/>

<Input type = "button" "value =" Modify InnerText "onclick =" EditInnerText () "/>

<Input type = "button" "value =" Modify InnerHTML "onclick =" EditInnerHTML () "/>

<Div id = "divMain"> </div>

<Input type = "button" value = "dynamic Add button" onclick = "CreateButton ()"/>

</Body>

</Html>

I hope this article will help you design javascript programs.

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