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.com/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.