1. 程式人生 > >jquery 獲取及操作元素 (常用)

jquery 獲取及操作元素 (常用)

.com pre img htm color hello fun 文檔 wid

jquery 獲取元素 參考:http://www.w3school.com.cn/jquery/jquery_selectors.asp

1.(1)$(document).ready() -->$()-->$ 整個文檔

(2)$("p") $("button") html標簽

(3)$(this) 當前對象

1 $(function(){
2   $("p").click(function(){
3     $(this).hide();
4   });
5 });

  (4)根據id獲取對象 $("#divs")

  (5)根據class獲取對象 $(".clsdiv")

  (6)根據name獲取對象 $("[name=‘sql‘]")

2.操作

  (1)$("[name=‘sql‘]").css({"color" : "gray"});

  (2)

 1 $(document).ready(function(){
 2   $("#btn1").click(function(){
 3     $("#test1").text("Hello world!");
 4   });
 5   $("#btn2").click(function(){
 6     $("#test2").html("<b>Hello world!</b>");
7 }); 8 $("#btn3").click(function(){ 9 $("#test3").val("Dolly Duck"); 10 }); 11 });

  (3)attr

1 $("button").click(function(){
2   $("img").attr("width","180");
3 });
1 $(selector).attr({attribute:value, attribute:value ...})
2 $(selector).attr(attribute,function(index,oldvalue))
3 $(selector).attr(attribute,value)
$(selector).attr(attribute)

$(".clsdiv")

jquery 獲取及操作元素 (常用)