2

祝大家美好的一天,对不起,如果你觉得这个问题很愚蠢。谁能帮助我如何更改打印输出的字体?他们说 .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>
4

3 回答 3

5

我建议如下:

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);

JS 小提琴演示

或者:

fontSizeChange(document.getElementById('display-date'), 4);

JS 小提琴演示

或者(如果要使用默认设置,可以省略factor参数):

fontSizeChange('display-date');

JS 小提琴演示

而且,当然,你也可以缩小 thngs:

fontSizeChange('display-date',0.25);

JS 小提琴演示

当然,您也可以使用 CSS:

#display-date {
    font-size: 2em;
}

JS 小提琴演示

允许将调整大小附加到返回的元素 ( document.querySelector()/ document.getElementById()) 或节点列表 ( 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);

JS 小提琴演示

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);

JS 小提琴演示

参考:

于 2013-04-28T15:08:08.100 回答
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>
于 2013-04-28T15:12:56.513 回答
0

您可以使用style属性从 javascript 更改 css 属性

document.getElementById('display-date').style.fontSize = '2em'   
于 2013-04-28T15:08:57.963 回答