开发者

Why jQuery matches elements that even doesn't have specified CSS attributes & values?

开发者 https://www.devze.com 2023-03-13 06:04 出处:网络
<html> <head> <script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js\"></script>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script>
    $(function () {
        var elements = $("*").filter(func开发者_运维百科tion () {
            return $(this).css("font-weight") == "700";
        }).append("Yes it has 'font-weight'");
    });

    </script>
</head>
    <body>
        <b>Bold text</b>
    </body>
</html>

The "b" tag is appended with the text Yes it has 'font-weight' even though that it's doesn't actually have this CSS attribute. Can anybody tell me why and how to avoid this please?


The b tag implicitly has a font-weight of 700 since it is the default font weight for bold text. This is the default value it has, unless you change it.

I don't think there is a way to find out if a CSS attribute is explicitly set in a stylesheet, or set to its default value by the browser. The only 'solution' (though I'd first like to know what the actual problem is) would be to change the font-weight for 'b' and 'strong' tags in your style sheet.


The <b> tag does have the style (unless you explicitly removed it).

Many of the HTML tags have default styles applied by the browser, like <h1>, <i>, and <b>. The selector is correct.

To get rid of the default styles (reset the page completely so that it has no styles whatsoever, even browser defaults), you can include a CSS reset.

Yahoo kindly supplies a pretty good one: http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css

0

精彩评论

暂无评论...
验证码 换一张
取 消