Home>Article>Web Front-end> How to put specified value into div in javascript

How to put specified value into div in javascript

青灯夜游
青灯夜游 Original
2021-10-18 16:52:42 6358browse

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 ";".

How to put specified value into div in javascript

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:

     

Rendering:

How to put specified value into div in javascript

##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中文网!"; }

Rendering:


How to put specified value into div in javascript##[Recommended learning:

javascript advanced tutorial

The 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn