javascript - 用自定义 html 替换文本
问题描述
我想在我的聊天应用程序中显示短信对话。
我的情况:当用户在搜索框中搜索某些文本时,包含 searchText 的消息将是红色样式。例子:
user search: "div"
// messageText = "abc <div>all is text</div>" (searching in plain text here);
// htmlText = "abc <div>all is text</div>";
// OUTPUT MUST BE: "abc <<span style='color: red'>custom</span>>string <<span style='color: red'>custom</span>>"
这是我的代码:
const origin = "abc <div>all is text </div>"; // text input
const str = "abc <div>all is text </div>"; // after sanitize
const element = document.createElement("div");
element.innerHTML = str;
for (let node of element.childNodes) {
// I want replace "div" text with my custom html
node.textContent = node.textContent.replace(/div/gi, "<span style='color: red'>custom</span>");
}
const result = element.innerHTML;
console.log(result);
这是result
输出
输出
abc <<span style='color: red'>custom</span>>string </<span style='color: red'>custom</span>>
预计
abc <<span style='color: red'>custom</span>>string <<span style='color: red'>custom</span>>
你能帮我吗,谢谢你的帮助。
解决方案
node.textContent = ...
通过转义您传递给它的任何内容,将产生有效的文本。
如果您想插入 HTML,请使用node.innerHTML
node.innerHTML = node.textContent.replace(/div/gi, "<span style='color: red'>custom</span>");
编辑 我意识到你的问题比这更复杂。您首先需要对文本中的 HTML 进行转义,然后替换div
为要插入的 HTML,最后用于inneHTML
应用结果。
Edit2 更新您的问题后,我了解您想在文本输入中搜索/突出显示某些内容。编辑代码来做到这一点
根据这个答案:
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function highlightSearch(node, query) {
//Within node, highlight matched text (using replacement "$&")
//highlight done by surrounding found text with a <span>
let txt = node.textContent; //Get inner text
console.log('raw text:', txt);
txt = escapeHtml(txt); //Escape text with HTML entities
console.log('html text:', txt);
//Search and replace ("$&" = whole matched substring)
txt = txt.replaceAll(query, "<span style='color: red'>$&</span>");
console.log('highlighted:', txt);
//Show result
node.innerHTML = txt; //<-- innerHTML
}
document.querySelector('#btn-search').addEventListener('click', e => {
//Read query
let q = document.querySelector('#search-query').value;
console.log('raw query:', q);
//Make search query html
q = escapeHtml(q);
console.log('html query:', q);
//Perform search/highlight
document.querySelectorAll('.search-in').forEach(node => {
highlightSearch(node, q);
});
});
button {
margin-top: 1em;
}
<p class="search-in">abc <div>all is text </div></p>
<p class="search-in">a <div> is not a </div>. but I can find 2 <div></p>
<input type="search" id="search-query" value="<div>" />
<button id="btn-search">Search</button>
推荐阅读
- reactjs - 当我在 create-react-app 中输入 npm start 时如何指定自定义 URL
- python - 如何根据世代显示,x '攻击' 和 y '防御'
- javascript - 在加载组件中检查后表达式已更改
- python - 如何修复 Anaconda Jupyter Notebook 内核错误?
- javascript - 使用 React-Native 在 ScrollView 中的特定位置缩放
- php - 插入在本地工作,但不在服务器上
- laravel - 将数据传递到包含的刀片视图
- amazon-web-services - 如何为 aws_autoscaling_group 发出的现场请求应用标签?
- javascript - 如何使用 ajax 和 django 上传文件?
- c++ - 判断 SQLite 数据库是否被锁定