大家好日子, 对不起,如果你发现这个问题愚蠢。任何人都可以帮助我如何更改打印输出的字体?他们说.style.fontSize,但我不知道如何在该代码中使用它。任何帮助将非常感激。谢谢!
<html>
<body>
<div id="display-date">
<script language="javascript">
today = new Date();
var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var yr = today.getYear();
if(yr < 1900) yr += 1900;
document.write(today.getDate() + " " + month[today.getMonth()] + " " + yr);
</script>
</div>
</body>
</html>
答案 0 :(得分:4)
我建议如下:
document.getElementById('display-date').style.fontSize = '2em'; // or whatever measurement.
而且,因为我有一些时间,这是一个功能:
function fontSizeChange(elem, factor) {
if (!elem) {
return false;
}
else {
factor = factor || 2; // 2 is the default
elem = elem.nodeType && elem.nodeType === 1 ? elem : document.getElementById(elem);
var cur = window.getComputedStyle(elem, null).fontSize,
size = parseInt(cur.replace(/\D+/g,''),10),
unit = cur.replace(/\d+/g,'');
elem.style.fontSize = (size * factor) + unit;
}
}
这样打电话:
fontSizeChange('display-date', 3);
或者:
fontSizeChange(document.getElementById('display-date'), 4);
或者(如果你想使用默认设置,你可以省略factor
参数):
fontSizeChange('display-date');
当然,你也可以收缩:
fontSizeChange('display-date',0.25);
当然,您也可以使用CSS:
#display-date {
font-size: 2em;
}
一个更新版本,允许将调整大小附加到返回的元素(document.querySelector()
/ document.getElementById()
)或nodeList(document.getElementsByTagName()
/ document.querySelectorAll()
):
Object.prototype.resize = function (prop, factor) {
if (prop) {
factor = parseFloat(factor) || 2;
var self = this.length ? this : [this],
cur, size, unit;
for (var i = 0, len = self.length; i < len; i++) {
var cur = window.getComputedStyle(self[i],null)[prop],
size = parseFloat(cur.replace(/\D/g,'')),
unit = cur.replace(/\d+/g,'');
self[i].style[prop] = (size * factor) + unit;
}
}
return this;
};
document.getElementsByTagName('div').resize('width',3);
document.getElementById('display-date').resize('font-size', 2.5);
Object.prototype.resize = function (prop, factor) {
if (prop) {
factor = parseFloat(factor) || 2;
var self = this.length ? this : [this],
cur, size, unit;
for (var i = 0, len = self.length; i < len; i++) {
var cur = window.getComputedStyle(self[i],null)[prop],
size = parseFloat(cur.replace(/\D/g,'')),
unit = cur.replace(/\d+/g,'');
self[i].style[prop] = (size * factor) + unit;
}
}
return this;
};
document.querySelectorAll('div').resize('height',3);
document.querySelector('#display-date').resize('font-size', 0.5);
参考文献:
答案 1 :(得分:0)
您可以使用style
属性更改javascript的css属性
document.getElementById('display-date').style.fontSize = '2em'
答案 2 :(得分:0)
您可以执行类似以下的操作
,而不是使用document.write<style>
.my_style_class
{
// add your font styling information here
font-size: small;
}
</style>
<div id="display-date" class="my_style_class">
</div>
<script>
document.getElementById("display-date").innerHTML = your_function_which_gets_the_date();
</script>