jQuery的一些特性和用法

 
1.精准简单的选择对象(dom):

$('#element');// 相当于document.getElementById("element") $('.element');//Class $('p');//html标签 

$("form > input");//子对象 

$("div,span,p.myClass");//同时选择多种对象 

$("tr:odd").css("background-color", "#bbbbff");//表格的隔行背景 $(":input");//表单对象 

$("input[name='newsletter']");//特定的表单对象 

2.对象函数的应用简单和不限制:

 element.function(par);  

$(”p.surprise”).addClass(”ohmy”).show(”slow”)... 

3.对已选择对象的操作(包括样式):  

$("#element").addClass("selected");//给对象添加样式 

$('#element').css({"background-color":"yellow",font-weight":"bolder" });//改变对象样式 

$("p").text("Some new text.");//改变对象文本 

$("img").attr({ src: "test.jpg", alt: "Test Image" });//改变对象文本 $("p").add("span");//给对象增加标签 

$("p").find("span");//查找对象内部的对应元素 $("p").parent();//对象的父级元素 

$("p").append("<b>Hello</b>");//给对象添加内容

4.支持aJax,支持文件格式:xml/html/script/json/jsonp

$("#feeds").load("feeds.html");//相应区域导入静态页内容

 $("#feeds").load("feeds.php", {limit: 25}, function(){alert("The last 25 entries in the feed have been loaded");});//导入动态内容

 4.对事件的支持:

 $("p").hover(function () { $(this).addClass("hilite");//鼠标放上去时},

function () { $(this).removeClass("hilite");//移开鼠标

});//鼠标放上去和移开的不同效果(自动循环所有p对象)

 5.动画:

 $("p").show("slow");//隐藏对象(慢速渐变) 

$("#go").click(function(){ 

  $("#block").animate({ 

      "90%", 

     height: "100%", 

     fontSize: "10em" 

  }, 1000 ); 

});//鼠标点击后宽、高、字体的动态变化

6.扩展:

 $.fn.background = function(bg){ 

   return this.css('background', bg); 

}; 

$(#element).background("red"); 

如果要为每一个jQuery 对象添加一个函数,必须把该函数指派给$.fn,同时这个函数必须要返回一个this(jQuery 对象)

 

原文地址:https://www.cnblogs.com/f19huangtao/p/4843102.html