jQuery的几点笔记

1.jQuery核心选择器 (sizzle.js) http://sizzlejs.com/

2.jQuery有两个主要特性

①隐式迭代
//改变页面所有p标签的背景色
$('p').css('backgroundColor','yellow');

②链式编程  
//当执行破坏链式编程的方法时,可以通过end()方法修复
$(this).prevAll().css('color','red').end().nextAll().css('color','blue');

 3.jQuery中大多数都是方法而不是属性(实现链式编程)

var str = $('#id').val();//获取值,也是调用的方法
$('#id').val('我是被设置的值');//设置值

 4.attr()和prop()

attr ->attribute , prop->property

$(this).attr('checked'); //this.getAttribute(); this.setAttribute(); 在网页中直接看到的值,得到的是 string 类型
$(this).prop('checked'); //this.checked 经过计算后的值,得到的是 Boolean 类型

5.jQuery对象 和 dom对象

//jQuery对象本身就是一个dom对象的集合
jQuery对象转dom对象 $('#btn')[0]或$('#btn').get(0)

 6.jQuery选择器部分元字符

,
.
空格 find()//所有后代
> children()//直接后代
+ next()//下一个兄弟
~ nextAll()
siblings()//所有兄弟
prev()//上一个兄弟
prevAll()

nextUntil() prevUntil()

//状态过滤
:disabled
:enabled
:checked
:selected
:input//<input><select><textarea>等

:header
:first//first()
:last//last()
:even
:odd
:gt(1)//gt()
:eq(1)//eq()
:lt(1)//lt()
:not(某种选择器)//not()

//属性过滤
[attr=xxx]
[attr!=xxx]
[attr^=xxx]
[attr*=xxx]
[attr$=xxx]
[attr1=xxx][attr2=yyy]

 7.提交到表单数据

//不要再对表单一个一个取值提交了,可以对一个表单进行序列化 得到键值对数组
$.post('提交地址',$('#表单id').serializeArray(),function(ret){
    //回调代码
});

//支持表单上传文件
var form = new FormData(document.getElementById("表单id"));
$.post('提交地址',form,function(ret){
    //回调代码
});

  

 

原文地址:https://www.cnblogs.com/techliang666/p/7921589.html