无法在IE8中获取fontStyle属性

时间:2014-06-10 09:25:48

标签: javascript jquery

您好我正在创建一个Web应用程序,我正在创建一个这样的字体属性:

    var text = document.createElement('font');
    text.id = "jsFonts";
    text.style.fontStyle = fontStyle; // like "bold"
    text.style.fontName = fontName;  // arial
    text.style.fontSize = fontSize + 'pt'; 
    text.innerText = txt;

这在IE9及更高版本以及chrome和firefox中运行良好。 但是在IE8中我得到的错误是这样的:

Error: Could not get the fontStyle property. Invalid argument. 

有人可以为我推荐一些东西或者像polyfill吗?

1 个答案:

答案 0 :(得分:0)

尝试使用fontWeight,更改:

text.style.fontStyle = fontStyle

text.style.fontWeight = "bold";

注意::已在HTML5中删除<font>元素,不应再使用该元素。相反,应该使用CSS属性。

添加:: 可能你可以检查属性是否受支持::喜欢:

if ('fontStyle' in document.body.style) { //returns true if its supported
    text.style.fontStyle = fontStyle;
}
else {
    text.style.fontWeight = fontStyle;
}