使用 js 访问 class 的内容

accessing the content of a class using js

我需要用js知道一个class的内容。 例如:-

<p style='visibility:hidden'>hello !</p>

如果我们写

alert(document.getElementsByTagName('P')[0].style.visibility)

它会 returns "hidden" ,但是如果我们写

<p class='peter'>hello !</p>
<style>.peter{visibility:hidden}</style>

它将 returns ""。 !!!!

elem.style 指的是它的 HTML style 属性,而不是它的计算样式。因此,使用 <style></style> 标签或 <link/ 标签声明的样式将被 计算 但它们不会影响 elem.style,因为它们不是元素 style 属性的值。只有 <sometag style="..."> 会影响 elem.style。要获得 computed 样式,请尝试使用 window.getComputedStyle(elem).