JQuery中如何获取样式属性的值?

如我想要获取样式名为“top”的div层的背景图片的url值,那么我在<script></script>中该如何写。
代码如下:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<style>
.top{background:url(Tupian.jpg) no-repeat;width:75px;height:90px;}
</style>
<script></script>
</head>
<body>
<div class="top"></div>
</body>
</html>

css方法:

1、取得第一个段落的color样式属性的值。

$("p").css("color");

2、将所有段落的字体颜色设为红色并且背景为蓝色。

$("p").css({ color: "#ff0011", background: "blue" });

<html>
<head>
    <title>JQuery中如何获取样式属性的值</title>
    <script type="text/javascript" src="jquery.js"></script>
    <style>
        .top {
            background: url(Tupian.jpg) no-repeat;
            width: 75px;
            height: 90px;
        }
    </style>
    <script type="text/javascript">
        $(function () {
            var url = $("div.top").css("background-image");
            alert(url);
        })
    </script>
</head>
<body>
    <div class="top"></div>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-20
$("div.top").css("background-image");