jQuery

参见网址:http://jquery.cuishifeng.cn/

jQuery:
转换:
jquery对象[0] => Dom对象
$(Dom对象) => jquery对象
选择器,直接找到某个或者某类标签
1.id = "i1"
$("#i1")找到id="i1"的标签
2.class
<div class="c1"></div>
$(".c1")
3.标签
<div id="i1" class="c1">
<a>f</a>
<a>t</a>
</div>
<div class="c2">
<a>z</a>
</div>

$("a")找到所有a标签
$(".c2")找到所有class=c2标签
$("#i1,.c2,a")分别找到所有id=i1,class=c2,a标签
$("#i1>a")找到id=i1下的所有a标签
$("#i1>a:first")找到id=i1下的所有a标签中的第一个
$("#i1>a:last")找到id=i1下的所有a标签中的最后一个
$("#i1>a:eq(0)")找到id=i1下的所有a标签中的第零个

关于自定义的属性
例如:
<div alex="123"></div>
<div alex="456"></div>
$("[alex]")找到属性名为alex的标签
$("[alex="123"]")找到属性名为alex并且值为123的标签

关于input系列
<input type="text" />
<input type="text" />
<input type="file" />
<input type="password" />
$("input[type="text"]")相当于$(":text")
实例:
多选,反选,全选
- 选择权
-
$("#tb:checkbox").prop("checked"); 获取checked属性的值
$("#tb:checkbox").prop("checked",true); 设置checked属性的值为true
-JQuery方法内置循环
$("tb:checkbox").each(function(k){
//k为当前索引
//this为DOM对象,当前循环元素 $(this)
})
- var v = 条件 ? 真值 : 假值

-筛选器
$(this).next() 下一个标签
$(this).nextAll()
$(this).nextUntil("#i1")

$(this).prev() 上一个标签
$(this).prevAll()
$(this).prevUntil("#i1")

$(this).parent() 父标签
$(this).parents()
$(this).parentsUntil()

$(this).child() 子标签
$(this).siblings() 兄弟标签
$("#i1").find("#i2") 子子孙孙中查找包含i2的标签
$(this).addClass()
$(this).removeClass()
            
-文本操作
$(..).text #获取文本内容
$(..).text("<a>1</a>") #设置文本内容
$(..).html #获取标签
$(..).html("<a>1</a>") #设置标签
$("input[name="s1"]").val() #获取input标签中value额值
$("input[name="s1"]").val(..) #设置input标签中value的值

-样式操作
addClass $(".c1").addClass("hide") #增加
removeClass $(".c1").removeClass("hide") #移除
toggleClass $(".c1").toggleClass("hide") #有hide则移除,否则增加hide

-属性操作
#专门用于做自定义的属性
$(..).attr("n") #获取相应标签中属性n的值
$(..).attr("n","v") #设置相应标签中属性n的值为
$(..).removeAttr("n") #移除相应标签中属性n

#专门用于checkbox,radio
$(..).prop("checked") #获取checked属性的值
$(..).prop("checked",true) #设置checked属性的值为true,即使得checked属性存在
ps:
index获取索引位置

-位置
$(window).scrollTop() 获取浏览器窗口的位置
$(window).scrollTop(0) 设置浏览器窗口的位置
offset().left 指定标签在html中的坐标
offset().top 指定标签在html中的坐标
position() 指定标签相对父标签(relative)标签的坐标
$("i1").height() 获取标签的高度

-事件
绑定方式:
jquery:
$(".c1").click(function(){})
*********************
$(".c1").bind("click",function(){})
$(".c1").unbind("click",function(){})
*********************
$(".c1").delegate("a","click",function(){}) #a为a标签
$(".c1").undelegate("a","click",function(){}) #a为a标签
*********************
$(".c1").on("click",function(){})
$(".c1").off("click",function(){})
原文地址:https://www.cnblogs.com/cansun/p/8531305.html