【发布时间】: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