Name Selector trong jQuery



Miêu tả

Name Selector (Bộ chọn tên phần tử) trong jQuery sẽ chọn tất cả phần tử với tên thể đã cho.

Cú pháp

Sau đây là cú pháp để sử dụng Name Selector trong jQuery:

 $('tagname')

Tham số

Dưới đây miêu tả các tham số được sử dụng trong Name Selector này:

  • tagname − Bất kỳ tên thẻ HTML chuẩn nào như div, p, em, img, li ...

Quảng cáo

Trả về

Giống như bất kỳ selector khác trong jQuery, Selector này cũng trả về một mảng các phần tử được tìm thấy.

Ví dụ

  • $('p') − Chọn tất cả phần tử với tên thẻ là p trong tài liệu.

  • $('div') − Chọn tất cả phần tử với tên thẻ là div trong tài liệu.

Ví dụ sau sẽ chọn tất cả các khối và áp dụng màu vàng cho màu nền của chúng:

<html>
   <head>
      <title>The Selecter 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() {
            /* This would select all the divisions */
            $("div").css("background-color", "yellow");
         });
      </script>
   </head>
	
   <body>

      <div class="big" id="div1">
         <p>This is first division of the DOM.</p>
      </div>

      <div class="medium" id="div2">
         <p>This is second division of the DOM.</p>
      </div>

      <div class="small" id="div3">
         <p>This is third division of the DOM</p>
      </div>

   </body>
	
</html>
Quảng cáo

Nó sẽ cho kết quả sau:

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


selector_trong_jquery.jsp