css(properties) trong jQuery



Miêu tả

Phương thức css(properties) trong jQuery thiết lập một đối tượng key/value như là các thuộc tính style tới tất cả các phần tử đã so khớp.

Cú pháp

Sau đây là cú pháp cho css(properties) trong jQuery:

selector.css( properties )

Cú pháp trên có thể được viết như sau:

selector.css( {key1:val1, key2:val2....keyN:valN})

Tham số

Dưới đây miêu tả chi tiết về các tham số được sử dụng trong phương thức css(properties) trong jQuery:

  • properties − Các cặp Key/value để thiết lập như là các thuộc tính style.

Quảng cáo

Ví dụ

Sau đây là ví dụ đơn giản minh họa cách sử dụng của phương thức css(properties) trong jQuery:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://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>" + color + "</span>.");
               $("#result").css({'color': color, 'font-weight' : 'bold', 'background-color': 'gray'});
            });
         });
      </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>
Quảng cáo

Nó sẽ cho kết quả:

Các bài học jQuery khác tại VietJack:


css_selector_trong_jquery.jsp