【问题标题】:Changing the CSSOM css style properties in javascript在 javascript 中更改 CSSOM css 样式属性
【发布时间】:2020-12-30 16:49:05
【问题描述】:

我正在尝试使用 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: Codepen

请告诉我应该如何解决这个问题。

【问题讨论】:

  • 这样的? document.getElementById("toy-pictures-latest").style.display = "inline";
  • 是的:style.display = 'inline'
  • 我在codepen中试过here我得到:TypeError: document.getElementById(...) is null
  • 你使用的是'getElementById',但在元素中你使用的是class而不是id
  • 使用:document.querySelector('.toy-pictures-greatest').style.display = 'inline'

标签: javascript css cssom


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2018-10-06
    • 2013-10-21
    • 2012-04-25
    相关资源
    最近更新 更多