【问题标题】:jquery .css('text-decoration') doesn't work from parentsjquery .css('text-decoration') 对父母不起作用
【发布时间】:2011-03-27 11:33:01
【问题描述】:

我必须获得元素所需的 css 属性(属性可以从它的父级继承)

标准 jquery .css() 可以做到。

所以我有这样一个 HTML:

<div id="container"> some text<br>
        <span id="some">Our element</span>
   <br> some text
</div>

<div id="result">
</div>

css:

#some {
    font-weight:bold;
}

#container {
    text-decoration: underline;
    color: rgb(0,255,0);
    font-size: 32px;
}

和 jQuery 代码:

$(function() {
    var decor = $("#some").css('text-decoration');
    var weight = $("#some").css('font-weight');
    var color = $("#some").css('color');
    var size = $("#some").css('font-size');
    var result = "<br><b>our object's own property:</b>"
    result += "<br>font-weight:" + weight + "<br>";
    result += "<br><b>inherited properties:</b><br>";
    result += "color: " + color + "<br>font-size: " + size + "<br>text-decoration: " + decor;

    $("#result").html(result);
});

显示所有属性,但文本装饰 - 不显示。但为什么? http://jsfiddle.net/aQYs2/

【问题讨论】:

    标签: jquery css text-decorations


    【解决方案1】:

    text-decoration 在 css 规范中使用 Inherited:no 定义,但您在示例中使用的其他属性使用 Inherited:yes 定义

    见:http://www.w3.org/TR/CSS2/text.html#propdef-text-decoration

    所以一切都很好:)

    【讨论】:

    • 你不知道文本装饰的替代方法:下划线。没有文本装饰,还有哪些其他 css 属性可以下划线?因为我需要继承=)
    • 不,但#container *{text-decoration:inherit;} 应该这样做。
    猜你喜欢
    • 1970-01-01
    • 2014-02-25
    • 2021-12-30
    • 2017-04-11
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 2017-09-23
    • 2020-02-09
    相关资源
    最近更新 更多