目录

css( name )

描述 (Description)

css( name )方法返回第一个匹配元素的样式属性。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.css( name )

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • name - 要访问的属性的名称。

例子 (Example)

以下是一个简单的例子,简单地显示了这种方法的用法 -

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("div").click(function () {
               var color = $(this).css("background-color");
               $("#result").html("That div is <span style = 'color:" + 
                  color + ";'>" + color + "</span>.");
            });
         });
      </script>
      <style>
         div { width:60px; height:60px; margin:5px; float:left; }
      </style>
   </head>
   <body>
      <p>Click on any square:</p>
      <span id = "result"> </span>
      <div style = "background-color:blue;"></div>
      <div style = "background-color:rgb(15,99,30);"></div>
      <div style = "background-color:#123456;"></div>
      <div style = "background-color:#f11;"></div>
   </body>
</html>

这将产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018