我有这些值从Firebase检索。看起来像这样

Progress : lowback:1:0:12-8-2019 -- 8:21:57 PM:VAS:10 -- difficulty:-1 -- 1:12-8-2019 -- 8:22:28 PM:VAS:10 -- difficulty:-1 -- 2:12-8-2019 -- 8:22:56 PM:VAS:10 -- difficulty:-1 -- 3:12-8-2019 -- 8:25:52 PM:VAS:10 -- difficulty:-1 -- 4:12-8-2019 -- 8:27:10 PM:VAS:10 -- difficulty:-1

请注意,单词难度一词在字符串中出现的次数过多。这是DOM元素的textContent属性。这是一个h6。

我怎样才能使困难一词变成一个大胆的词。它没有跨度或任何html标记,我可以使用getElementById来选择它。

var progressText = childSnapshot.child("userProfile/progress");
var StringifiedProgressText = JSON.stringify(progressText);

var progress = document.createElement("h6");

progress.textContent = "Progress : " + StringifiedProgressText;
progress.textContent = progress.textContent.replace(/{/g, "");
progress.textContent = progress.textContent.replace(/}/g, "");
progress.textContent = progress.textContent.replace(/"/g, "");
progress.textContent = progress.textContent.replace(/,/g, " -- ");

最佳答案

是否允许使用.innerHTML属性而不是.textContent?如果是,则可以执行以下操作:

.....
let temp = "Progress : " + StringifiedProgressText;
temp = temp.replace(/{/g, "");
temp = temp.replace(/}/g, "");
temp = temp.replace(/"/g, "");
temp = temp.replace(/,/g, " -- ");

progressTextBold = temp.replace(/difficulty/g, "<b>difficulty</b>");

progress.innerHTML = progressTextBold;

10-07 21:41