【问题标题】:The .style of my class is null in javascript我的班级的 .style 在 javascript 中为空
【发布时间】:2022-01-30 13:39:27
【问题描述】:

我有一个简单的网页,顶部有一个 div:

<div class="divmain-login">
    <div class="divmain-sup-login">
        <div class="textos">
            <h1>Manejo de archivos</h1>
            <h2>Gaspar Giménez 2021</h2><h3>v 1.2</h3>
        </div>
    </div>
</div>

我想用这个 JS 代码获得这个块的属性高度(使用 .divmain-sup-login 类):

function setDivSupHeight(){
const DivSup = document.querySelector(".divmain-sup-login");
console.log(DivSup.getAttribute("style"));

    const divh = DivSup.style.height;
    console.log(divh);
}

    setDivSupHeight();

控制台返回这个:

空(第 3 行)

什么都没有(第 7 行)

属性样式返回null,这是CSS:

.divmain-sup-login{
position: sticky;
background-color: #F56D7B;
text-align: center;
width: 100vw;


height: 90px;
    display: flex;
    flex-flow: column wrap;
    justify-content: center;
    align-content: center;
}

我不知道如何获得这个块的高度。

【问题讨论】:

    标签: javascript html css class height


    【解决方案1】:

    Element.getAttribute('style')Element.style 指的是元素的 style="..." 属性 - 不是来自样式规则或最终计算样式的任何内容,在您的情况下,该属性确实没有使用,因此它的值是 null .

    您实际上可能想要的是Element.getComputedValue().getProperty(),请参阅https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 2018-05-20
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多