首页 > 解决方案 > 文字本身不会加粗

问题描述

我正在尝试使用变量创建一个A元素链接,并且当我尝试将文本加粗时。它只是返回<b>App: </b>而不是App:

代码:

//Set Webpage App Title
function setAppTitle() {
    const header = document.getElementById('header');
    const a = document.createElement('a');
    const appText = 'App: '.bold()
    const text = document.createTextNode(appText + appName);
    a.setAttribute('href', "http://example.com/applicaton?id=");
    a.setAttribute('id', "appTitle");
    a.appendChild(text);
    header.appendChild(a);
}

标签: javascripthtml

解决方案


不推荐使用 string的方法,并且只用和.bold包围字符串。请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/bold<b></b>

如果您想在函数中手动构建 html,只需b自己创建一个元素:

//Set Webpage App Title
function setAppTitle() {
    const header = document.getElementById('header');
    const a = document.createElement('a');
    const b = document.createElement('b');
    const appText = 'App: '
    const text = document.createTextNode(appText + appName);
    a.setAttribute('href', "http://example.com/applicaton?id=");
    a.setAttribute('id', "appTitle");
    a.appendChild(text);
    b.appendChild(a)
    header.appendChild(b);
}

推荐阅读