jquery入门汇总

1,选择器:

基本选择器:

$("p"),  $("#btn"),  $(".class"),  $(this),  $("p.class")

层次选择器:

$("div>span")div中的所有span,     $("div~span")div后的所有 span元素           $("div+span")div后的第一个span元素

*基本过滤选择器:""

$("div:first"), $("div:last"),   $("div:not()"),   $("div:gt(2)"),  $("div:eq(2)"), $("div:lt(2)"),  $("div:even"), $("div:odd"), $("div:animate"),    $(":header"),       $(":focus") 

**内容过滤选择器:

$("div:contains(‘我’)"),$("div:has(p)"),  $("div:empty")不包含子元素的div,  $("div:parent")拥有子元素的div

**可见性过滤选择器:

$("div:hidden"), $("div:visible");

**属性过滤选择器:

$("div[id]"),  $("div[id=test]")

**子元素过滤选择器:

$("ul li:first-child"),  $("ul li:last-child"), $("ul li:only-child");

**表单对象选择器:

$("input:checked"), $("select options:selected"),  $("#form:disabled"), $("#form:enabled");

**表单选择器:

$(":input"), $(":text"), $(":submit"), $(":button"), $(":checkbox"), $(":radio"), $(":image"), $(":file"), $(":reset"), $(":password");

2,事件:

$("#btn").click()   dblclick()    mouseenter()   mouseleave()    mousedown()   mouseup()  hover()   focus()  blur()失焦

3,函数:

**显示/隐藏   

$("ul li:first-child").hover(function(){  $("p").hide()   show()  toggle()  })

**淡入淡出

$(":button").mousedown(funciton(){ $("[href]").fadeIn()  fadeOut()  fadeTaggle()  fadeTo()颜色变淡 }) 

fadeIn(slow, opacity, function);

**滑进滑出

$("a[target="_blank"]").mouseenter(function(){

$("tr:even").slideDown()  slideUp() slideToggle();});

**动画:

$("button").dblclick(function(){

$("div").animate({left:100px;});} ) 需要给div设置position

让动画停止:

$("#btn").click(function(){
$("div").stop();})

4,jquery的链式操作:

$("button").css("color","red").slideUp(1000).slideDown(3000);

其原理是每次执行完毕一个操作,函数返回this指针:

function S(){ }

S.prototype={

fn:function(){
console.log(1);

return this;},

fn1:function(){
console.log(2);

return  this;}}

var  n=new S();

n.fn().fn1();  // 1 2

5,获取内容:

$(".div1").html()  获取带标签的内容

$(".div1").text()  获取不带标签的内容

$(".input").val()  获取input,button等表单中的输入值;

6,获取,设置元素的属性:

$(".div1").attr("class");        $(".div1").attr("class","div2")    

7,回调函数:允许添加的函数

hide(300, function(){ })  ,    attr("href", function(){} )

8,添加文本内容:

前增(还在这个文本内部):$(".btn").click(funciton(){  $(".div1").prepend("<p>这是一段文字</p>")})

后增(还在这个文本内部):append()

前增(不在文本中):before() 

后增(不在文本中):after()

9,删除元素:

删除这个元素:$("div").remove();

删除这个元素中的容: $("div").empty();

过滤掉某个元素: $("div").remove(".div1");      //class name 为div1的div不被删除

10,操作class name:

添加class name: $("div").addClass("div1 div2");

删除class name: $("div ").removeClass("div1");

切换class name: $("div").toggleClass("div3");

11,操作css:

$(".btn").css({"color":"red","width":"100px"});

12,获取元素宽度,长度:

$(".div").width()     innerWidth() 包括内边距    outerWidth()包括内边距和外边框

13,遍历:

**向上遍历:

一个祖先:$("span").parent();

所有的祖先:$("span").parents();

一定范围内的祖先:$("span").parentsUntil("div");

**向下遍历:

直系儿子:$("div").child();

直系中的某个儿子:$("div").child("p.1");

寻找后代中某些:$("div").find("span");

返回所有后代:$("div").find("*");

-------------emmmm,遍历同胞。。。。。--------------

同级别所有同胞:$("div").siblings().css("color":"red");

同级别下一个同胞:next();        

同级别自己以下所有同胞:nextAll();    

  prev()    prevAll()  prevUntil()

14,过滤:

选中的第一个:$("div p").first() 

选中的最后一个:$("div p").first();

选中特定序号:$("div").eq(2)  选中第三个div

选中有这个特征的元素:$("div").filter(".classname")  

选中没有这个特征的元素:$("div").not(".classname");

15,jquery的ajax异步请求:

必须部署在http服务器中,可以使用xampp,放在hotdocs中运行;

load(url):

$("div").load("tt.txt");

get(url, callback):

$.get("test.php", function(data, status){alert(data+status);})

post(url, data, callback):

$.post("tt.php",{name:"bob",age:12},function(data,status){alert(data+status);});

猜你喜欢

转载自blog.csdn.net/kalinux/article/details/82666029