jQuery - css(name, value) 方法



描述

css(name, value) 方法為所有匹配的元素設定單個樣式屬性的值。

語法

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

selector.css( name, value )

引數

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

  • name - 要設定的屬性名稱。

  • value - 屬性的值。

示例

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

<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>" + color + "</span>.");
               $("#result").css("color", color);
            });
				
         });
      </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.