javascript - html中的span元素中粗体标签不生效

标签 javascript html css

我对 html、css 和 javascript 完全陌生。

我正在尝试创建一个简单的利率计算器,并且我想设置结果的样式,以便结果消息中的数字突出显示,如下所示:

Interest : If you deposit 10,
at an interest rate of 10.25.
You will receive an amount of 11.025,
in the year 2023.

我得到的是<b>呈现为文本的元素,例如:

Interest : If you deposit <b>10</b>,
at an interest rate of <b>10.25</b>.
You will receive an amount of <b>11.025</b>,
in the year <b>2023</b>.

我的代码是这样的:

function compute()
{
    var principal = document.getElementById("principal").value;
    var rate      = document.getElementById("rate").value;
    var years     = document.getElementById("years").value;
    var interest  = principal * (rate / 100) * years;
    // add amount to principal
    let amount    = interest + parseInt(principal);
    var year      = new Date().getFullYear() + parseInt(years);

    var interestString = "If you deposit " + principal.bold() + ",\n at an interest rate of " + rate + ".\n You will receive an amount of " + amount + ",\n in the year " + year + ".";
    document.getElementById("result").innerText = interestString
}
body {
    background-color:black;
    font-family: arial;
    color:white
}

h1 {
    color: grey;
    font-family: verdana
}

.maindiv {
    background-color: white;
    color:black;
    width:300px;
    padding: 20px;
    border-radius: 25px;
    margin: auto;
}
<div class="maindiv">
    <h1>Simple Interest Calculator</h1>
    Amount <input type="number"  id="principal">  
    <br/>
    Rate <input type="range" id="rate" min="1" max="20" step="0.25" value="10.25"  oninput="this.nextElementSibling.value = this.value">
    <output>10.25</output>   
    <br/>
    No. of Years 
    <select id="years">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    <button onclick="compute()">Compute Interest</button>
    <br>
    <br>
    Interest : <span id="result"></span>
</div>

我很高兴能找到解决这个问题的方法。谢谢!

最佳答案

innerText 更改为 innerHTML

关于javascript - html中的span元素中粗体标签不生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70941439/

相关文章:

html - 需要交叉而不是在 SVG 中打勾

html - 使子元素充满滚动父元素的水平宽度

html - 海报图片上的文字没有响应

javascript - 网页设计 : Client wants a rotation-based navigation bar

javascript - 比较 ajax 的响应 - jQuery

javascript - 如何将字符串转换为 JavaScript 日期对象?

javascript - 动态迭代嵌套对象并使用递归将特定值推送到单独的数组中

html - 尝试使用 Node.js 提供带有图像的 html 页面时收到 GET 错误

html - 悬停在标签上,但不显示菜单

javascript - 从下拉菜单上的子类别中获取父级