JavaScript-为什么innerHTML属性包含HTML时显示为文本?

问题描述 投票:0回答:1

我在ASP.CORE View中执行以下JS代码,旨在基本上设置页面上元素的内容。

if (notificationBanner) {
    var bannerText = unescape("@ViewData.GetClient().GetText("$.resetCredentials.step2.otpSendBanner")");
    console.log(bannerText);
    notificationBanner.innerHTML = bannerText;
}

以下内容正在浏览器控制台中登录:

<p>A One Time Password (OTP) has been sent to your mobile number below. Enter the OTP in the field below. <strong>The OTP is valid for 10 minutes</strong>.</p>


元素最终以这种方式结束:

enter image description here

但是这是不正确的,我希望<strong></strong>中的部分为粗体。为什么将其添加为文本?

javascript innerhtml
1个回答
0
投票

我只是在这里提出一个官方解决方案。功劳完全归功于蜡笔小新。一种方法是将文本插入textarea元素,然后从那里提取文本。它看起来像这样:

if (notificationBanner) {
    var bannerText = $('<textarea />').html(unescape("@ViewData.GetClient().GetText("$.resetCredentials.step2.otpSendBanner")")).text();
    console.log(bannerText);
    notificationBanner.innerHTML = bannerText;
}
© www.soinside.com 2019 - 2024. All rights reserved.