【发布时间】:2017-07-13 22:46:36
【问题描述】:
尝试在字符串中设置| 的样式,但将完整的标签添加到字符串中,知道如何使用纯 Javascript 完成此任务吗?
ctrl.js
var string = "[2017-07-12T12:38:36-04:00]|zsh458|info|<n/a>|[infra.actorRouter] Processing message from topic UEQ: 216014da-b849-4a1d-8232-3629e58338ce { partition: 0, offset: 12081530 }"
var message = string.replace(/\|/g,'<span class="pipe">|</span>');
console.log(message); //printing this
[2017-07-12T12:38:36-04:00]<span class="pipe">|</span>zsh458<span class="pipe">|</span>info<span class="pipe">|</span><n/a><span class="pipe">|</span>[infra.actorRouter] Processing message from topic UEQ: 216014da-b849-4a1d-8232-3629e58338ce { partition: 0, offset: 12081530 }
【问题讨论】:
-
控制台不是用来显示渲染的 HTML,只显示纯文本。
-
将
message放入元素的innerHTML中,您会看到它的样式正确。 -
@Barmar 你能提供答案吗?
标签: javascript html css angularjs regex