javascript - 如何更换
使用 javascript 的空间?
问题描述
在正文中搜索所有实例<div> <br></div>
并使用 JavaScript 替换为空格
解决方案
只需对元素使用String.replace()
方法即可。.textContent
你并不完全清楚是否div
应该更换整个或只是
它的内部。以下是处理两者的示例:
仅替换
:
// Get all the elements that need work into an array
let elements = Array.prototype.slice.call(document.querySelectorAll("div"));
// Loop over the array and replace the HTML entity with a space char.
elements.forEach((el) => { el.textContent = el.textContent.replace(" ", " "); });
<div> <br></div>
<div> <br></div>
<div> <br></div>
<div> <br></div>
替换整个div
:
let el = document.getElementById("elementToSearch");
// Just replace all occurences of the element with a space
el.innerHTML = el.textContent.replace("<div> <br></div>", " ");
/* You won't see this style applied anywhere because
all the div elements have been removed*/
#elementToSearch div { width:400px; height:100px; background-color:yellow; }
<div id="elementToSearch">
<div> <br></div>
<div> <br></div>
<div> <br></div>
<div> <br></div>
</div>
推荐阅读
- php - 找不到类 'App\Http\Controllers\VarificationEmail' laravel_version 6+
- javascript - JavaScript - 拆分长代码行的问题
- java - 弱引用对象不会被垃圾回收
- r - 使用 `dplyr::na_if` 有可能创建缺失数据?
- python - 寻找 3D 线性回归线的斜率
- wordpress - 将whatsapp分享按钮添加到php插件
- matplotlib - plt.fill_between()方法的解释?讨论
- ruby-on-rails - Ruby on Rails 和 Sendinblue 通过 SMTP
- java - Maven Shade 插件不会重新定位公共日志记录
- python - 如何让 Python Discord 机器人等待某个测验的答案?