首页 > 解决方案 > 在 javascript 中更改 CSSOM css 样式属性

问题描述

我正在尝试使用 style.cssText 来显示使用 javascript 的隐藏段落。这是我的代码:

html:

<p class="toy-pictures-latest">Latest Toy Pictures</p>
         <p class="toy-pictures-greatest">Greatest Toy Pictures</p>

CSS:

.toy-pictures-latest{
    display: none;
}

.toy-pictures-greatest{
        display: none;
}

js:

toy-pictures-latest.style.cssText = 'display: inline;';

我也试过

document.getElementById("toy-pictures-latest").style.cssText = 'display: inline;';

请参阅代码笔: Codepen

请让我知道我应该如何解决这个问题。

标签: javascriptcsscssom

解决方案


js:

document.getElementById("toy-pictures-latest").style.display = "inline";

html:

<p id="toy-pictures-latest">Latest Toy Pictures</p>
  

CSS:

#toy-pictures-latest{
    display: none;
}

推荐阅读