Aui*_*ine 0 html javascript css jquery
哦,是的,关于这个规则的另一个问题.那我们在这里有什么?如果我使用jQuery来获得像这样的行高:
$elm.css('lineHeight')
Run Code Online (Sandbox Code Playgroud)
我总是得到已计算的值,例如20px.它来自font-size 10和2 line-height.
但如果我这样得到线高:
elm.style.lineHeight
Run Code Online (Sandbox Code Playgroud)
我得到了未计算的实际价值
问题是 - 如何使用jQuery获取行高规则的值?为什么jQuery会返回计算值!?
小智 5
elem.style只返回一个元素的声明内联样式而不是实际应用的样式.请参阅https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style
style属性对于一般学习元素的样式没有用,因为它只表示元素的内联样式属性中设置的CSS声明,而不是来自其他地方样式规则的CSS声明,例如节中的样式规则
<head>或外部样式床单.要获取元素的所有CSS属性的值,您应该使用它window.getComputedStyle().
jQuery.css()返回计算出的样式
描述:获取匹配元素集中第一个元素的计算样式属性.
要使用jQuery获取内联样式,您必须通过查询属性attr('style').
你可以使用这样的函数:( http://andreknieriem.de/jquery-checken-ob-ein-element-ein-bestimmtes-inline-style-attribut-hat/)
(function ($) {
$.fn.inlineStyle = function (prop) {
var styles = this.attr("style"),
value;
styles && styles.split(";").forEach(function (e) {
var style = e.split(":");
if ($.trim(style[0]) === prop) {
value = style[1];
}
});
return value;
};
}(jQuery));
Run Code Online (Sandbox Code Playgroud)