js dynamically changes div content

I wrote some divs before;

DIV related programming summary_div programming_bcbobo21cn's blog-CSDN blog

Graphical DIV related programming examples - Programmer Sought 

There are styles; there are also js dynamic operations;

Let's continue to learn and see how js dynamically changes the text in the div;

Initially run as follows;

 

After clicking the button as follows;

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>动态改变div内容</title>
</head>
<body>

<button onclick="setdiv()">click me</button>
<div id="div1">
    AAAAA。
</div>
 
<script>
    var str="Hello Javascript!";
	function setdiv() {
        document.getElementById('div1').innerHTML=str;
	}
</script>

</body>
</html>

 div is a square area on a web page; each web page element has its id, where the id of div is "div1";

The onclick attribute of the web page button specifies the javascript statement or function to be executed; here specifies the execution of the js function setdiv();

In the setdiv() function, the document.getElementById('xxx') method of js gets the webpage element through the id, and then specifies its innerHTML attribute as a new value; the innerHTML attribute is often the text displayed on the webpage element;

 

Guess you like

Origin blog.csdn.net/bcbobo21cn/article/details/131843596
Recommended