在javascript中获取元素只是第一步,如何对元素的属性进行更改才是接下里的重要步骤,本文主要讲述了如何在js中更改dom元素的内容。
HTML表单内容:
- nbsp;html>
Document
登录后复制
1.textContent:
- const box=document.querySelector(".box"); console.log(box); const p=document.querySelector('p'); console.log(p); //textContent:添加文本 p.textContent ="hello world";
登录后复制
2.innerText:
- const box=document.querySelector(".box"); console.log(box); const p=document.querySelector('p'); console.log(p); p.innerText ="php.cn";
登录后复制
3.innerHTML:
- const box=document.querySelector(".box"); console.log(box); const p=document.querySelector('p'); console.log(p); //将html字符串渲染出来应该使用innerHTML p.innerHTML='php.cn';
登录后复制
推荐:《2021年js面试题及答案(大汇总)》
以上就是如何在JS中改变DOM元素的文本内容的详细内容,更多请关注【创想鸟】其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。