Javascript method of putting values into divs: 1. Use the innerTexts attribute, the syntax "div object.innerText="specified value";"; 2. Use the innerHTML attribute, the syntax "div object.innerHTML=" Specify the value ";".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript puts the specified value into the div
Method 1: Use the innerTexts attribute
The innerText property can set or get all the text in the label (and its sub-labels), but will not get the label (or can filter out all labels). If there are multiple spaces or newlines, it will be parsed as one space.
If you want to clear the content of the label, innerText = ""
; that's it
If you want to set the content of the label, innerText = "Fill in the desired The label and content to be set "
; when setting the content, all the original content will be overwritten. However, the tag will not be parsed and will be printed directly on the page as text.
Implementation code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> div{ height: 50px; border: 1px solid red; } </style> </head> <body> <div id="demo"></div><br> <input type="button" onclick="addtext()" value="点击按钮,向div中添加内容" /> <script type="text/javascript"> function addtext() { var div=document.getElementById("demo"); div.innerText="hello"; } </script> </body> </html>
Rendering:
##Method 2: Using innerHTML attribute
This method can get all the content in the label, including labels, spaces, text, newlines, etc. If you want to clear the content of the tag, innerHTML = ""; that's it If you want to set the content of the tag, innerHTML = "Fill in the tag and content you want to set"; set the content , all original content will be overwritten. Implementation code:function addtext() { var div=document.getElementById("demo"); div.innerHTML="欢迎来到PHP中文网!"; }
##[Recommended learning:
javascript advanced tutorialThe above is the detailed content of How to put specified value into div in javascript. For more information, please follow other related articles on the PHP Chinese website!