使用粗体文本创建元素和设置内容

时间:2016-06-21 18:58:50

标签: jquery html

如果我没有完全缺乏这种语言的经验,我会提前道歉。我有一个返回天气数据的查询(工作正常)。我想要做的就是设置某些格式化HTML元素,例如粗体/斜体/下划线。

在下面的代码中,我希望位置,条件和可见性为 BOLD 。有没有办法在下面的代码块中做我想做的事情?

    $('.weather ul').append(
        $('<li />', {
            text: 'Location: ' + Location + ' Conditions: ' + weathernow + ', Visibility: ' + visibility_mi
        })
    );

当前输出:地点:Chicopee,MA条件:清晰,可见性:10.0米

所需产出:位置: Chicopee,MA 条件:清除,可见性: 10.0m

编辑:我应该提到我已经尝试在我认为会去的各个地方添加<B>标签,但它对我不起作用。它会将<B>显示为文本。

尝试:

    $('.weather ul').append(
        $('<li />', {
            text: '<b>Location: </b>' + Location + ' Conditions: ' + weathernow + ', Visibility: ' + visibility_mi
        })
    );

结果: 地点: Chicopee,MA条件:清除,可见性:10.0mi

1 个答案:

答案 0 :(得分:3)

变化:

text: 'Location: ' + Location + ' Conditions: ' + weathernow + ', Visibility: ' + visibility_mi

为:

html: '<b>Location:</b> ' + Location + ' <b>Conditions:</b> ' + weathernow + ', <b>Visibility:</b> ' + visibility_mi

使用text:,您无法添加HTML标记。因此,正如您所看到的,我已将其更改为html:,并在应显示为粗体的文本周围添加了<b>标记。