jQuery - css( name ) 方法



描述

css( name ) 方法返回第一個匹配元素的樣式屬性。

語法

以下是使用此方法的簡單語法:

selector.css( name )

引數

以下是此方法使用到的所有引數的描述:

  • name - 要訪問的屬性的名稱。

示例

以下是一個簡單的示例,展示了此方法的用法:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://tutorialspoint.tw/jquery/jquery-3.6.0.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>

這將產生以下結果:

jquery-css.htm
廣告

© . All rights reserved.