如何获得DIV子元素的computputedstyle?

发布于 2025-01-17 09:42:37 字数 1269 浏览 5 评论 0原文

抱歉,ESL的单词选择。我要获得的是与Div相邻(?)或相关(?)的H3的GetComputedStyle,该DIV具有特定类(主题),而H3则没有。

这是CSS的一个示例:

.mainBook 
{
     position: absolute;
}   
.mainBook h3
{
     line-height: 120px;
}

我知道如何获得主本书的ComcomputedStyle:

const element = document.querySelector('.mainBook');
const style = getComputedStyle(element);
// and then if I want: style.getPropertyValue("nameOfProperty")

这是HTML:

<div class="mainBook">
        <h3>Title</h3>
</div>

不确定这是否有帮助,但是:

const element = document.querySelector('.mainBook');
    const style = getComputedStyle(element);
    // and then if I want: style.getPropertyValue("line-height");
    // How do I getComputedStyle the h3?
.mainBook 
    {
         position: absolute;
    }   
    .mainBook h3
    {
         line-height: 120px;
    }
<div class="mainBook">
            <h3>Title</h3>
    </div>

但是有什么方法可以做同样的事情,但是对于H3?

谢谢!

Sorry in advance for the ESL choice of words. What I'm trying to get is the getComputedStyle of an h3 that's adjacent (?) or related (?) to a div which has a specific class (mainBook), while the h3 hasn't got any.

Here's an example of the CSS:

.mainBook 
{
     position: absolute;
}   
.mainBook h3
{
     line-height: 120px;
}

I know how to getComputedStyle of mainBook:

const element = document.querySelector('.mainBook');
const style = getComputedStyle(element);
// and then if I want: style.getPropertyValue("nameOfProperty")

Here's the HTML:

<div class="mainBook">
        <h3>Title</h3>
</div>

Not sure if this helps but:

const element = document.querySelector('.mainBook');
    const style = getComputedStyle(element);
    // and then if I want: style.getPropertyValue("line-height");
    // How do I getComputedStyle the h3?
.mainBook 
    {
         position: absolute;
    }   
    .mainBook h3
    {
         line-height: 120px;
    }
<div class="mainBook">
            <h3>Title</h3>
    </div>

But is there any way to do the same but for h3?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2025-01-24 09:42:37

1) 您可以获取 h3 元素,

const element = document.querySelector('.mainBook h3');

并从 compulatedStyle 获取其 lineHeight ,如下所示:

const style = getComputedStyle(element);
let lineHeight = style.lineHeight.

const element = document.querySelector('.mainBook h3');
const style = getComputedStyle(element);
console.log(style.lineHeight)
.mainBook {
  position: absolute;
}

.mainBook h3 {
  line-height: 120px;
}
<div class="mainBook">
  <h3>Title</h3>
</div>

2) 您还可以从 mainBook 元素获取 h3 元素,如下所示:

const mainBookEl = document.querySelector('.mainBook');
const headingEl = mainBookEl.querySelector('h3')
const style = getComputedStyle(headingEl);
const lineHeight = style.lineHeight;

const mainBookEl = document.querySelector('.mainBook');
const headingEl = mainBookEl.querySelector('h3')
const style = getComputedStyle(headingEl);
console.log(style.lineHeight)
.mainBook {
  position: absolute;
}

.mainBook h3 {
  line-height: 120px;
}
<div class="mainBook">
  <h3>Title</h3>
</div>

1) You can get h3 element as

const element = document.querySelector('.mainBook h3');

and get its lineHeight from computedStyle as:

const style = getComputedStyle(element);
let lineHeight = style.lineHeight.

const element = document.querySelector('.mainBook h3');
const style = getComputedStyle(element);
console.log(style.lineHeight)
.mainBook {
  position: absolute;
}

.mainBook h3 {
  line-height: 120px;
}
<div class="mainBook">
  <h3>Title</h3>
</div>

2) You can also get h3 element from mainBook element as:

const mainBookEl = document.querySelector('.mainBook');
const headingEl = mainBookEl.querySelector('h3')
const style = getComputedStyle(headingEl);
const lineHeight = style.lineHeight;

const mainBookEl = document.querySelector('.mainBook');
const headingEl = mainBookEl.querySelector('h3')
const style = getComputedStyle(headingEl);
console.log(style.lineHeight)
.mainBook {
  position: absolute;
}

.mainBook h3 {
  line-height: 120px;
}
<div class="mainBook">
  <h3>Title</h3>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文