You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在给定JavaScript代码中修改输出的字体、字号并设置加粗?

修改JavaScript输出的字体、字号和粗体的方法

嘿,我来帮你搞定这个样式调整的问题!你要给JS输出的内容改字体、字号还加粗,其实不用复杂操作——直接在document.write()的输出内容外层套个带样式的HTML标签就行,浏览器会自动解析这些标签来渲染样式。

具体来说,你可以用<span>标签包裹要输出的文本,然后通过style属性设置字体(font-family)、字号(font-size)和粗体(font-weight: bold)。下面是修改后的完整代码:

<script>
var today_obj=new Date()
var today_date=today_obj.getDate()
var tips=new Array()
var tiptitle=''
tips[1]='2-1'
tips[2]='1-1'
tips[3]='2-0'
tips[4]='0-1'
tips[5]='3-2'
// 给输出内容套上带样式的span标签
document.write('<span style="font-family: 微软雅黑; font-size: 18px; font-weight: bold;">' + tiptitle + '</span>')
document.write('<span style="font-family: 微软雅黑; font-size: 18px; font-weight: bold;">' + tips[today_date] + '</span>')
</script>

你可以根据自己的需求调整font-family(比如换成Arial、宋体)、font-size(比如20px、1.2rem)的值,粗体也可以用700代替bold,效果是一样的。

如果希望样式更统一好维护,也可以先在页面里定义一个CSS类:

<style>
.custom-output {
    font-family: 微软雅黑;
    font-size: 18px;
    font-weight: bold;
}
</style>

然后JS里的输出改成:

document.write('<span class="custom-output">' + tiptitle + '</span>')
document.write('<span class="custom-output">' + tips[today_date] + '</span>')

这样后续要改样式的话,直接修改CSS类就行,不用动JS代码~

内容的提问来源于stack exchange,提问作者Ashkan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:23:45