当前位置:网站首页>DOM series change element content
DOM series change element content
2022-07-20 12:14:00 【Old__ L】
Catalog
1、innerText
1.1、 summary
HTMLElement
Interface innerText
Attribute represents the rendered text content of a node and its descendants . As a getter, It is similar to the user using the cursor to highlight the element content , Then copy it to the text obtained from the clipboard . As setter, It will replace the child element of the element with the given value , Convert any line breaks to <br>
Elements .
1.2、 grammar
var renderedText = HTMLElement.innerText;
HTMLElement.innerText = string;
1.3、 Output value
a section DOMString
Represents the rendered content of an element . If the element itself is not rendered (e.g Deleted from the document or not displayed in the view ), At this time, the return value is the same as Node.textContent Same attribute .
1.4、 Example
<body>
<button> Display the current system time </button>
<div> Some time </div>
<p>1123</p>
<script> // When we hit the button , div The text will change // 1. Get elements var btn = document.querySelector("button"); var div = document.querySelector("div"); // 2. Registration events btn.onclick = function () {
div.innerText = "2019-6-6"; }; function getDate() {
var date = new Date(); // Let's write a 2019 year 5 month 1 Japan Wednesday var year = date.getFullYear(); var month = date.getMonth() + 1; var dates = date.getDate(); var arr = [" Sunday ", " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday "]; var day = date.getDay(); return " It's today :" + year + " year " + month + " month " + dates + " Japan " + arr[day]; } </script>
</body>
2、innerHTML
2.1、 summary
Element.innerHTML Property to set or get HTML The descendants of elements represented by syntax .
2.2、 grammar
const content = element.innerHTML;
element.innerHTML = htmlString;
2.3、 Return value
DOMString Containing descendants of elements HTML Sequence . Set the element's innerHTML All descendants of this element will be deleted and will be replaced with htmlString replace .
2.4、 abnormal
SyntaxError: When HTML When it is not marked correctly , Set up innerHTML Syntax errors will be thrown .
NoModificationAllowedError: When the parent element is Document when , Set up innerHTML You will be prompted that modification is not allowed .
2.5、 Example
<body>
<button> Display the current system time </button>
<div> Some time </div>
<p>1123</p>
<script> // When we hit the button , div The text will change // 1. Get elements var btn = document.querySelector("button"); var div = document.querySelector("div"); // 2. Registration events btn.onclick = function () {
div.innerHTML = getDate(); }; function getDate() {
var date = new Date(); // Let's write a 2019 year 5 month 1 Japan Wednesday var year = date.getFullYear(); var month = date.getMonth() + 1; var dates = date.getDate(); var arr = [" Sunday ", " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday "]; var day = date.getDay(); return " It's today :" + year + " year " + month + " month " + dates + " Japan " + arr[day]; } </script>
</body>
3、 difference
3.1、 The difference in getting content :
innerText Will remove spaces and line breaks , and innerHTML Spaces and line breaks are preserved
3.2、 The difference when setting content :
innerText Can't recognize html, and innerHTML identifies
4、 Example
<body>
<div></div>
<p>
I am character.
<span>123</span>
</p>
<script> // innerText and innerHTML The difference between // 1. innerText Don't recognize html label Nonstandard Remove spaces and line breaks var div = document.querySelector("div"); // div.innerText = '<strong> It's today :</strong> 2019'; // 2. innerHTML distinguish html label W3C standard Keep spaces and line breaks div.innerHTML = "<strong> It's today :</strong> 2019"; // These two properties are readable and writable You can get the content of the element var p = document.querySelector("p"); console.log(p.innerText); console.log(p.innerHTML); </script>
</body>
Postscript
If you feel the article is not good
//(ㄒoㄒ)//
, Just leave a message in the comments , The author continues to improve ;o_O???
If you think the article is a little useful , You can praise the author ;\\*^o^*//
If you want to progress with the author , Sure Wechat scan QR code , Focus on the front-end old L;~~~///(^v^)\\\~~~
Thank you readers(^_^)∠※
!!!
边栏推荐
- Appium automation test foundation - operating wechat applet
- DOM系列之改变元素内容
- transformers中BertPreTrainedModel使用说明
- Instructions for bertpretrainedmodel in transformers
- Hardware engineer test questions
- 【golang学习笔记1.7】 golang中的函数得使用
- Cloud foundry developer course (lfd232) 3 core concepts
- File, exception, module
- 双亲委派机制
- Game 302 of leetcode
猜你喜欢
HCIA-R&S自用笔记(11)VRP文件系统、系统管理
Cloud foundry developer course (lfd232) 3 core concepts
Chrome Basics
故障006:连接排序去重结果不如人愿
Okaleido or get out of the NFT siege, are you optimistic about it?
File upload vulnerability (I)
分数阶pid控制和矢量控制永磁电机
MySQL learning notes - stored procedures and functions
源码编译安装LAMP
Instructions for bertpretrainedmodel in transformers
随机推荐
opencv(12):cv::rectangle学习与代码演示,使用opencv画矩形/矩形框
360浏览器的收藏夹地址和历史地址
ERP capability planning and scheduling
Game 302 of leetcode
Chrome基础
Cylindricity error evaluation method based on MATLAB
1339:求后序遍历
信奥中的数学学习:小学、初高中数学 视频集
什么是反向代理?
Pyqt5 learning resource preparation and environment configuration
动环监控系统价格,动环监控系统价格多少
IPv6-基础
DOM系列之排他思想
Mathematics learning in Xinao: video collection of mathematics in primary school, junior high school and senior high school
ES6新增(一)let与常量
Dynamic ring monitoring module, dynamic ring monitoring module classification
DeFi 2.0的LaaS协议Elephant,重振DeFi赛道发展的关键
数据库的事务四大特性&&隔离级别总结(面试高频)
1381:城市路(Dijkstra)
23、网络原理——TCP/IP四层模型之中的重点协议(2)