首页 文章

为什么以下Javascript代码(设置CSS样式)不起作用?

提问于
浏览
1

我正在尝试使用以下内容设置对象的CSS样式:

document.getElementById(obj).style='font-weight:bold; color:#333;';

但它不起作用 . 有谁知道为什么?

3 回答

  • 6

    你可以分开

    document.getElementById(obj).style.fontWeight='bold';
    document.getElementById(obj).style.color='#333';
    
  • 0

    您需要将样式的基础 cssText 设置为folows:

    document.getElementById('xx').style.cssText='font-weight:bold; color:#333;';
    
  • 8

    文档的样式属性是一个对象,请参考W3school上的HTML DOM Style Object

相关问题