【问题标题】:Javascript get style of a element in all state [duplicate]Javascript在所有状态下获取元素的样式[重复]
【发布时间】:2018-03-14 13:09:39
【问题描述】:

我想获取一个元素的行高,就这么简单。

我知道这行得通

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv" style="line-height:30px;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

但我真正的问题是 - 如果将样式应用于父元素,我们能否获得元素的行高?

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="line-height:30px;">
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

或者如果它是浏览器的默认值,如何获取它。?

console.log(document.getElementById("myDiv").style.lineHeight);

console.log($("#myDiv").css('lineHeight'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

但在所有这些情况下,jQuery 返回我需要的值,但我需要使用 javascript 获取值。

任何帮助将不胜感激。

【问题讨论】:

  • 嗯,jQuery 是 JavaScript。您可能在使用getComputedStyle 方法..?
  • @Justinas 请在将其标记为重复之前阅读问题。
  • @webBer 我读了这个问题,它似乎是那个问题的副本。 edit您的问题,以便更清楚地说明为什么它不是重复的。
  • @webBer,在 jQuery 中,.css() 获取匹配元素集中第一个元素的计算(表示当前,实际)样式属性的值,或者为每个匹配的元素设置一个或多个 CSS 属性。 (来源:api.jquery.com/css)-它回答了您的“真正问题”(参见您的帖子)……而在 vanilla JS 中,getComputedStyle() 方法做同样的事情。
  • @MikeMcCaughan 答案可能对我有用,但两者都是不同的问题。在那里,他想要在样式表中应用 CSS 样式,但在我的情况下,我想要元素的默认行为或元素从父元素继承的样式。!

标签: javascript html css


【解决方案1】:

你可以使用getComputedStyle方法来获取样式...这实际上是在jquery中使用的...

//console.log(document.getElementById("myDiv").style.lineHeight);

//console.log($("#myDiv").css('lineHeight'));


var elem = document.getElementById("myDiv");
var view = elem.ownerDocument.defaultView;

console.log(view.getComputedStyle( elem )['line-height']);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div id="myDiv">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>

【讨论】:

  • ...这与副本中的回答相同。
  • 嗯,和现有的一样。
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 2012-10-21
  • 2014-10-31
  • 1970-01-01
相关资源
最近更新 更多