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

How to put specified value into div in javascript

青灯夜游
Release: 2021-10-18 16:52:42
Original
6472 people have browsed it

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:

<!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>
Copy after login

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中文网!";
}
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template