Truy cập đối tượng Jquery

Truy cập đối tượng Jquery

- Truy cập đối tượng
  Thẻ mặc định   $("p")  $("h1") ...
  Thẻ class   $(".tenClass")
  Thẻ id $("#tenClass")


- Truy cập theo tên name
   $('[DK tìm]')

$('[name=Ngay]').val("Giá trị..")

 
$form.find( "input[name='s']" ) 
  $form.find( ".tenClass" )

  $("tên_tag[tên_thuộc_tính=Giá_trị]")
  $("input[name=TenO]").val()

  $("input[name=SoTienDua]").val(10000);

 


Ví dụ:
    $("input[name=NgayThueMatBang_Gio]").val('gt');

  $(".tenclass").find( "input[type='text']" )

 

- Khai báo biến

<h1 class="aa"></h1>   
<script>
var b_h1= $("h1");
alert( b_h1.attr("class") );
</script>


Empty text
$("div").empty();
<div>ab</div> --> <div></div>
-> chỉ sử dụng cho html ...

-  Truy cập next và prev

$(this).next()
$(this).prev()

$("#Ten").next( "span" ).val('x');
$("#Ten").next( "span" ).css( "display", "inline" ).fadeOut( 1000 );

$("#Ten").nextAll();

 
- Truy cập theo tên thẻ

 <input name="a"  value="20" />

  ->  $("input[name='a']").val(100) ;

- Tìm đối tượng bên trong
$("#iddiv").find( "#theone" ).text("Gia tri khi tim thay")

Tìm số lượng div trong div lớ
var _find_item = $("#monhangcho").find("#item_"+obj._SYS_KEY);
if(_find_item.length==0)
{
...
}

- Truy cập theo chỉ số thứ tự
  $("thẻ").eq(index)
Chỉ số tính từ 0
Ví dụ
<p>My name is Donald (index 0).</p>
<p>Donald Duck (index 1).</p>
<p>I live in Duckburg (index 2).</p>
<p>My best friend is Mickey (index 3).</p>
...
  $("p").eq(1).css("background-color", "yellow");

Ví dụ 2:

<input name="bt_t" type="button" value="1" />
<input name="txt_t" type="text" />

<input name="bt_t" type="button" value="2" />
<input name="txt_t" type="text" />

<input name="bt_t" type="button" value="3" />
<input name="txt_t" type="text" />

<script>
      $('[name=bt_t]').on("click", function(){
            var index = $( "[name=bt_t]" ).index( this );
            //alert(index);
            $( "[name=txt_t]" ).eq(index).val(index);
         } );
</script>

Ví dụ 3: lọc theo vòng lặp
<script>
    $( '[name=bt_t]' ).each(function( index ) {
              alert( $('[name=bt_t]').eq(index).val()   );
    });
</script>


 - Truy cập theo Filter
  $("thẻ").filter("điều kiện")

<script>
$(document).ready(function(){
  $("p").filter(".intro").css("background-color", "yellow");
});
</script>
<h1>Welcome to My Homepage</h1>
<p>My name is Donald.</p>
<p class="intro">I live in Duckburg.</p>
<p class="intro">I love Duckburg.</p>
<p>My best friend is Mickey.</p>

- Truy cập theo loại trừ Not
  $("thẻ").Not("điều kiện")

<script>
$(document).ready(function(){
  $("p").not(".intro").css("background-color", "yellow");
});
</script>
<h1>Welcome to My Homepage</h1>
<p>My name is Donald.</p>
<p class="intro">I live in Duckburg.</p>
<p class="intro">I love Duckburg.</p>
<p>My best friend is Mickey.</p>

- Truy cập this

   $(this).val("...");
Chỉ dùng trong sự kiện của đối tượng.

$(".chk").click(function() {
 
 alert(   $(this).val()   );
});
* Chú ý: phải để dấu $(this)

 
Truy cập cấp cha
   $("h1").parent();

 - Truy cập cấp con
   $( "div" ).children().css( "border-bottom", "3px double red" );

-  Truy cập name trong thẻ div
var firstname = jQuery("#div1 input[name=firstname]").val();




 - Truy cập tự động các thành phần trong FORM

<script>
$(document).ready(function(){
  $("button").click(function(){
    $("div").text($("form").serialize());
  });
});
</script>

<form action="">
  First name: <input type="text" name="FirstName" value="Mickey"><br>
  Last name: <input type="text" name="LastName" value="Mouse"><br>
</form>
<button>Serialize form values</button>
<div>Ket qua:FirstName=....&LastName=....</div>

 

Truy cập đối tượng Jquery