2014-06-10 52 views
0

你好我创建这里我创建一个字体属性这样一个Web应用程序:无法获取的fontStyle属性在IE8

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. 

任何人都可以建议我这个东西或类似填充工具?

回答

0

尝试使用fontWeight,变化:

text.style.fontStyle = fontStyle 

text.style.fontWeight = "bold"; 

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

新增:: 可能是你可以检查是否支持的属性::像:

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

好奏效。我可以使用一些polyfills来设置它吗? – user3725375

+0

@ user3725375检查更新的答案,但仍然使用CSS会好得多选项 –

+0

好吧谢谢sudhir – user3725375