jQuery

jQuery

  • jQuery实现了一套与原生js一样的方法处理,只不过方法缩短内容,更简易,适用多个浏览器
  • 要想使用jquery方法就必须是jquery对象。不能使用DOM对象操作jquery方法
  • 在jQuery中 $就是jQuery对象
function jQuery(){ 

} 

var $=jQuery; 
jQuery对象和DOM对象 (页首) 

var divs = document.querySelectorAll("div") // DOM对象列表 

$("div")//jQuery对象列表 

DOM 转换为jQuery (页首) 

var div=document.querySelector("div"); 

console.log( $(div))//DOM变成jquery 

jQuery变成DOM元素 (页首) 

console.log($("div")[0]) 

console.log($("div").get(0)); 

var arr=Array.from($("div")); 

console.log(arr) 

 

 
 

选择器 (页首) 

 

$("div");        // 元素选择器,返回多个元素  

$(".div1");      // 类(class)选择器,返回多个元素 

$("#div1");      // ID选择器,返回单个元素 

$("div.div1");// 交集选择器 

$("div .div1");// 后代选择器 

$(".div1+.div2");// 兄弟选择器 

$(".div1~div");// 同辈选择器 

$(".div1>.div2");// 子代选择器 

 

属性选择器 (页首)HTMLCSS中不区分大小写,所以所有标签属性命名通常不能使用驼峰式命名法 

必须使用单词与-连接 box-aa

 

$("div[name]")// 属性选择器 

$("div[name=a]") // 匹配给定的属性是某个特定值的元素 

$("div[name!=a]")// 匹配所有不含有指定的属性,或者属性不等于特定值的元素。 

$("div[name^=a]")// 匹配给定的属性是以a开始的元素 

$("div[name$=a]")// 匹配给定的属性是以a结尾的元素 

$("div[name*=a]")// 匹配给定的属性是以包含a的元素 

$("div[name|=a]")// name的值是a,或者值是a起头后面紧随一个-的字符 
//<div name = 'a-b'></div><div name = 'a'></div>

$("div[name~=a]")// name值是a或者name中有a并且前后用空格区分的 
 //<div name = 'a b'></div>
$("li:first").css("color","red");//把所有li放在一个列表中,选择这个列表的第一个 

$("li").first() 

$("li:first-child").css("color","red");//选择当前选择器父元素的第一个子元素,如果是li则选择,如果不是不选择 

$("li:first-of-type").css("color","red");//选择当前选择器父元素的第一个是li的子元素,如果第一个不是li就向下继续查找 

 
$("li:last") 

$("li").last() 

$("li:last-child") 

$("li:last-of-type") 

 

$("li:nth-child(1)")//选择当前选择器父元素的第n个子元素,如果是li则选择,如果不是不选择 

$("li:nth-of-type(1)")//选择当前选择器父元素的第n个是li的子元素,如果第一个不是就向下继续查找 

// 获取的到子元素从1开始 

$("li:nth-child(odd)").css("color","red"); //odd奇数 

$("li:nth-child(even)").css("color","red");//even偶数 


$("li:nth-of-type(odd)") 

$("li:nth-of-type(even)") 


 // 获取li形成的列表,列表的下标从0开始 

$("li:even").css("color","red"); 

$("li:odd").css("color","red"); 


$("li:only-child") 

// 如果某个元素是父元素中唯一的子元素,那将会被匹配如果父元素中含有其他元素,那将不会被匹配。 

$("li:only-of-type") 

// 选择所有没有兄弟元素,且具有相同的元素名称的元素。如果父元素有相同的元素名称的其他子元素,那么没有元素会被匹配。 



$("li:not(.li0)").css("color","red");    // 去除所有与给定选择器匹配的元素,  不是li0的

$("li").not(".li0") 

 
//针对某一个li
$("li:eq(3)").css("color","red");//慢 

$("li").eq(3).css("color","red");//快 

 

// 下标大于2的元素 

$("li:gt(2)").css("color","red"); 

// 下标小于2的元素 

$("li:lt(2)").css("color","red"); 
// siblings()所有兄弟 
$("li.li0").siblings().css("color","red"); 

// 下一个相邻兄弟选择器 
$("li.li0").next()      

// 所有向下兄弟选择器 
$("li.li0").nextAll()// 向下兄弟选择到li1之前的所有兄弟 li0-li1之间的那个
$("li.li0").nextUntil(".li1").css("color","red"); 

// 相邻向上兄弟选择器 
$("li.li0").prev(); 

// 所有向上兄弟选择器 
$("li.li0").prevAll(); 

// 向上兄弟选择到li1之后的所有兄弟 
$("li.li1").prevUntil(".li0") 



// 获取所有的子元素 
$("ul").children() 

// 获取父元素 
$(".li0").parent(); 

//获取父元素列表,也可以获取父元素列表中的某个 
$(".li0").parents(); 

// li0的父元素列表中到body之前的所有元素 
$(".li0").parentsUntil("body"); 
$(":header")    // 获取所有h1-h6的元素 

$(":animated")// 获取所有animate动画 

$(":focus")     // 获取当前获取焦距的表单元素或者超链接 

 

// 选择空元素 
console.log( $("li:empty")) 

// 选择有子元素或者后代元素的元素 
console.log($("li:parent")) 

// 选择ul中子代、后代具备li0元素的ul 
console.log($("ul:has(.li0)")) 

// 子代和后代中文本内容包含3字符 
console.log( $("li:contains(3)")) 

 

// 隐藏标签,visibility: hidden;不算隐藏是可以见 
$(":hidden") 

// 所有可见标签 
$("visible") 


// 选择列表中第3个到第5个 
$("li").slice(3,5) 

 

// li列表中是否包含class是li0 这个返回的是布尔值,这个专用于判断class 
console.log($("li").hasClass("li0")) 

// li列表中是否包含某选择器的元素,返回布尔值  
console.log($("li").is(".li0")) 
表单选择器:
// $(":input") 

// $(":text") 

// $(":checkbox") 

// $(":radio") 

// ... 

// $("input:disable") 

// $("input:checked") 

 
遍历 

 

var arr=[3,4,5,6,7]; 

$.each(arr,function(index,value){ 

    console.log(index,value) 

}) 

 

var obj={a:1,b:2,c:3,d:4}; 

$.each(obj,function(key,value){ 

    console.log(key,value) 

}) 

 

var divs=document.querySelectorAll("div"); 

$.each(divs,function(index,elem){ 

    console.log(index,elem) 

}) 

 

$.each($("div"),function(index,item){ 

    console.log(index,item) 

}) 

 

$("div").each(function(index,item){ 

    console.log(index,item) 

}) 

 


设置文本内容 

 

$("div").text("你好"); //所有内容都写你好

 

// 设置不相同的内容 
var arr=["a","b","c","d"]; 
$("div").text(function(index,item){ 

    return arr[index]; 

}) 

 

console.log($("div").text());//等同于innerText 



// 设置html内容   等同于innerHTML 
$("div").html("<span></span>")
var arr=[ 
    {site:"网易",url:"http://www.163.com"}, 
    {site:"淘宝",url:"http://www.taobao.com"}, 
    {site:"腾讯",url:"http://www.qq.com"}, 
    {site:"京东",url:"http://www.jd.com"}, 
    {site:"什么值得买",url:"http://www.smzdm.com"}, 

] 
$("div").html(function(index,item){ 
    return `<a href='${arr[index].url}'>${arr[index].site}</a>` 
}) 
// 获取列表中第一个元素的html内容 
console.log($("div").html()) 




// .val 等同于value 
$("input").val("a"); 
$("input").val(function(index,value){ 
    return index;
}); 
// 获取input列表中第一个元素的value 
console.log($("input").val()) 
设置数据  

$("div").attr();//设置DOM标签属性 

$("div").prop();//设置DOM的对象属性 

$("div").data();//设置DOM的数据 


// DOM 是一个对象 

// DOM有标签属性,也有对象属性 

// 标签属性  设置标签属性setAttribute() 
//删除标签属性removeAttribute() 

var div=document.querySelector("div"); 

// DOM 对象属性 
div.ab=10; 

div["data-ab"] //只存在标签属性

//标签属性和DOM通用:
// div.id,div.className,input.type 
$('div')是个函数,每次返回的结果引用时不同的



//给所有div的a属性设置为10 
$("div").attr("a","10"); 

 

//给所有div的a属性设置为当前的索引 
$("div").attr("a",function(index,item){ 

    return index; 

}); 

 

//给所有div的a属性设置为1,b属性设置为2,c属性设置为3 

$("div").attr({ 

    a:1, 

    b:2, 

    c:3 

}); 

 

//给所有div的a属性设置为当前索引,b属性设置为当前索引+10,c属性设置为当前索引+100 

$("div").attr({ 

    a:function(index){ 

        return index; 

    }, 

    b:function(index){ 

        return index+10; 

    }, 

    c:function(index){ 

        return index+100; 

    } 

}); 

 

//移除所有div的a属性 

$("div").removeAttr("a") 

 

$("div") // 每次返回的结果引用是不同 

console.log( $("div")===$("div")) // false 

 

 

$(elem).prop 对象属性 (页首) 

 

//这样可以给DOM元素设置对象属性 

$("div").prop("ab",3); 

console.log($("div").prop("ab")) 

console.log($("div")[2].ab) 

 

$("div").prop({ 

    a:1, 

    b:2 

}) 

 

//删除对象属性 

$("div").removeProp("a") 

 

 

$(elem).data 设置数据属性 (页首) 

 

// 通过data添加的属性不会影响到DOM的标签或者DOM对象本身 

$("div").data("id","aaa"); 

console.log($("div")) 

//移除数据属性 

$("div").removeData("id"); 

 
jQuery (页首) 

jQuery中文API文档 

https://jquery.cuishifeng.cn/ 

1、css (页首) 

给div jQuery对象设置字体颜色为红色 

$("div").css("color","red") 

 

给div jQuery对象索引为0的字体设置为绿色,其他为红色 

$("div").css("color",function(index,value){ 

    return index ? "red" :"green"; 

}) 

 

给div jQuery对象列表设置宽度50px,高度50px,背景颜色红色 

$("div").css({ 

    width:50, 

    height:"50px", 

    backgroundColor:"red" 

}) 

 

给div jQuery对象列表索引为0的设置宽高为50px,背景颜色为绿色,其他宽高100px,背景色为红色 

$("div").css({ 

    width:function(index,value){ 

        return index ? 100 : 50 

    }, 

    height:function(index,value){ 

        return index ? 100 : 50 

    }, 

    backgroundColor:function(index,value){ 

        return index ? "red" :"green"; 

    } 

}) 

 

获取div jQuery对象列表内的第一个div 

获取当前元素的样式,可以获取行内样式,也可以获取css样式 

console.log($("div").css("width")); 

console.log($("div").css(["width","height","backgroundColor"])) 

 

给div jQuery对象列表添加div1类名,然后移除 

$("div").addClass("div1").removeClass("div1"); 

 

点击div jQuery对象,如果点击的目标有类名div2,移除,否则添加 

$("div").click(function(){ 

    $(this).toggleClass("div2"); 

}) 

 

css获取的是字符串,width获取的是数值 

console.log( $("div").width()) 

 

innerWidth获取到的是width-padding, 

获取第一个匹配元素内部区域宽度(包括补白、不包括边框)。 

$("div").innerWidth(200); 

console.log($("div").innerWidth()) 

 

outerWidth获取到的是width-padding-border 

获取第一个匹配元素外部宽度(默认包括补白和边框)。 

$("div").outerWidth(200) 

console.log($("div").outerWidth()) 

 

outerWidth参数设置为true时,获取的是width+padding+border+margin,只能获取不能设置 

参数设置为 true 时,计算边距在内。默认值是false 

console.log($("div").outerWidth(true)); 

 

如果有绝对定位就是left和top 

console.log($(".div1").position()) 

不能设置 错误 

$(".div1").position({left:200,top:200}); 

 

相对页面的左上角位置 

console.log($(".div1").offset()); 

可以设置 

$(".div1").offset({left:50,top:50}); 

 

获取和设置滚动条 

console.log($(document).scrollTop()) 

$(document).click(function(){ 

    $(document).scrollTop(100); 

}) 
2DOM操作 (页首) 

 

创建一个DOM的jQuery对象 

var div=$("<div></div>"); 

console.log(div) 

 

给body中添加一个div,返回body 

$("body").append("<div></div>") 

给body中添加一个div,返回这个添加的div 

$("<div></div>").appendTo("body").width(50).height(50).css("backgroundColor","red"); 

 

把div添加在body的最前面 

$("body").prepend("<div></div>"); 

$("<div></div>").prependTo("body"); 

 

给兄弟元素的上面插入 

$("div").before("<p></p>") 

$("<p></p>").insertBefore("div"); 

 

给兄弟元素的下面插入 

$("div").after("<p>111</p>"); 

$("<p></p>").insertAfter("div"); 

 

给span包裹一层div 

$("span").wrap("<div></div>") 

索引为0的包裹p,其他包裹div 

$("span").wrap(function(index){ 

    return index ? "<div></div>" : "<p></p>" 

}) 

 

给所有span外面包裹一层div 

$("span").wrapAll("<div></div>") 

 

给span中的所有子元素包裹一层a标签 

$("span").wrapInner("<a href='#'>111</a>") 

 

clone(是否复制事件和数据,是否复制后代元素的事件和数据) 

这个clone不管是否truefalse都是深复制 

默认值是clone(false,true) 

$("div").data("a","1").click(function(){ 

    $(this).width(200); 

}).clone(true).appendTo("body").mouseover(function(){ 

    console.log($(this).data("a")); 

}) 

 

 

$("div").data("a",1).click(function(){ 

    $(this).width(200); 

}).children().click(function(){ 

    $(this).css("color","black") 

}).data("b","2"); 

$("div").clone(true,false).appendTo("body") 

 

 

var div=$("div").click(function(){ 

        console.log("aaa") 

    }) 

除了将元素删除外,还将元素的事件删除 

    div.remove(); 

仅删除元素,不删除事件 

    div.detach(); 

删除div中的所有子代元素 清空子元素 

div.empty(); 

div.appendTo("body"); 

 

原生js,remove仅仅删除元素,并没有删除事件 

var div=document.querySelector("div"); 

div.addEventListener("click",clickHandler); 

div.remove(); 

document.body.appendChild(div); 

function clickHandler(e){ 

    console.log("aa") 

} 

 

将div替换为p标签 

$("div").replaceWith("<p></p>"); 

$("<p><p>").replaceAll("div"); 
3、事件 (页首) 

事件默认行为 (页首) 

表单的提交submit  跳转页面 

表单的重置reset  清除表单内容 

右键菜单contextmenu  弹出右键菜单 

图片禁拖标记  mousedown  设置e.preventDefault();就不会有禁拖标记 

文本禁拖标记 

 

addEventListener (页首) 

capture 捕获阶段触发 

once 只调用一次 

passive 禁止使用preventDefault 

div1.addEventListener("click",clickhandler1,{capture:true,once:true,passive:true}); 

 

阻止冒泡 

e.stopPropagation(); 

 

同一个DOM对象,同一个事件,多个函数时,阻止同事件后续函数的触发 

e.stopImmediatePropagation();  

 

 

jQuery中的事件 (页首) 

 

点击后清除div的事件 

$("div").on("click",clickHandler); 

function clickHandler(e){ 

    // console.log("aaa") 

    $(this).off("click",clickHandler) 

} 

 

原生js中 (页首) 

函数中arguments 

function fn(){ 

    // arguments.callee 当前函数 

    // console.log(arguments.callee) 

    // 回调当前函数外的上下文环境的函数 

    console.log(arguments.callee.caller); 

    // console.log(fn.caller) 

} 

function fn1(){ 

    fn(); 

} 

fn1(); 

 

回调函数中,如果直接执行参数函数,那么在回调函数中的this指向window或者undefined 

如果使用arguments[下标]() 这种使用arguments调用回调函数方法后,回调函数中this执行这个arguments 

function fn(f){ 

    // f(); 

    arguments[0](); 

} 

function fn1(){ 

    // console.log(this);//window || undefined(严格模式) 

    console.log(this);//回调当前函数的上下文环境函数的arguments 

} 

fn(fn1); 

 

fn.length  形参列表长度 

arguments.length 实参列表长度 

 

 

arguments.callee    当前函数 

arguments.callee.caller    调用当前函数的函数 

点击div后打印aaa并移除div的点击事件 

var div=document.querySelector("div"); 

div.addEventListener("click",function(){ 

    fns(); 

}) 

function fns(){ 

    console.log("aaa") 

    // div.removeEventListener("click",arguments.callee.caller) 

    div.removeEventListener("click",fns.caller) 

} 

 

jQuery中 (页首) 

直接删除 

$("div").on("click",function(){ 

    console.log("aa") 

    $("div").off("click"); 

}) 

 

使用这种方式会将所有的click事件删除 

$("div").on("click",function(){ 

    console.log("aa") 

    $("div").off("click"); 

}) 

$("div").on("click",function(){ 

    console.log("bb") 

}) 

 

 

可以给事件增加别名,命名空间 

$("div").on("click.a",function(){ 

    console.log("aa") 

    $("div").off("click.a"); 

}) 

$("div").on("click.b",function(){ 

    console.log("bb") 

}) 

 

 

var obj={ 

    init:function(){ 

        var a=1; 

        // on的第二个参数可以是一个传参值,这个值被传到事件e中的data当中 

        $("div").on("click",{a},this.clickHandler); 

    }, 

    clickHandler:function(e){ 

        console.log(e) 

    } 

} 

obj.init(); 

 

 

$("div").on("xietian",function(e,o){ 

    console.log("aaa") 

    console.log(o) 

}) 

// 相当于dispatchEvent抛发事件,第二个参数是传递给事件处理函数的附加参数 

$("div").trigger("xietian",{a:1,b:2}); 

 

$("div").on("xietian",function(e,a,b,c){ 

    // console.log("aaa") 

    // console.log(a,b,c); 

    return 10; 

}) 

// 相当于dispatchEvent抛发事件 

$("div").trigger("xietian",[5,6,7]); 

var a=$("div").triggerHandler("xietian",[5,6,7]); 

console.log(a) 

 

triggerHandler 不会冒泡, 

不会激活默认事件行为, 

只针对jQuery对象列表的第一个抛发事件 

可以获取返回值 

 

trigger 会冒泡, 

激活默认事件行为, 

针对jQuery对象列表的所有元素抛发事件 

 

jQuery事件简写 

$("div").click(function(){ 

}) 

$("div").mousedown(function(){ 

}) 

// 没有input简写事件 

$("form").input(function(){ 

}) 

 

网页加载顺序 (页首) 

1DOM树生成 

2CSS树生成 

3DOMCSS合并形成DOM渲染树 

4、加载图片等资源完成 

 

DOMCSS合并形成DOM渲染树以后触发 

$(document).ready(function(){ 

}) 

判断window加载完成,加载图片等资源完成以后触发 

window.onload=function(){ 

} 

 

一些事件 (页首) 

focus blur 是针对input,不能冒泡 

$("form").on("focus",function(){ 

    console.log("aa") 

}) 

focusin focusout 事件冒泡,可以针对父元素或者input都可以使用 

$("form").on("focusin",function(){ 

    console.log("aa") 

}) 

 

select 针对input和textArea中文本选中时激活 

$("input").on("select",function(e){ 

    console.log(this.selectionStart)//选中的开始位置 

    console.log(this.selectionEnd)//选中的结束位置 

    console.log(this.selectionDirection)//选择方向 

}) 

 

change 是针对所有表单元素,当value值被修改后,并且失去焦距时触发 
4、动画 (页首) 

 

隐藏 ,隐藏需要毫秒数,函数是执行完成动画后调用的函数 

$("div").hide(2000); 

$("div").hide(2000,function(){ 

    console.log("aaaa") 

}); 

 

连缀时,先隐藏再显示 

$("div").hide(2000).show(2000); 

 

$("div").click(function(){ 

    // 切换显示隐藏 

    $(this).toggle(2000); 

}) 

 

maxheight减到0再加到原来的 

通过高度变化隐藏和显示元素 

$("div").slideUp(2000).slideDown(2000).slideToggle(2000); 

 

通过透明变化隐藏和显示元素 

$("div").fadeOut(500).fadeIn(500); 

$("div").fadeTo(2000,0.5); 

$("div").fadeToggle(2000); 

 

用于创建自定义动画的函数 

$("div").animate({ 

    left:500, 

    top:300, 

    width:200 

},2000) 

$("div").animate({ 

    left:300 

},1000).animate({ 

    top:300 

},1000).animate({ 

    left:0 

},1000).animate({ 

    top:0 

},1000) 

 

 

5、插件 (页首) 

jQuery实例化方法插件 

(function(){ 

    $.fn.bgc=function(color){ 

        if(!color)return this.css("backgroundColor"); 

        this.css("backgroundColor",color); 

        return this; 

    } 

})(); 

$("div").width(50).height(50).bgc("red").on("click",function(){ 

    console.log("aaa") 

}) 

console.log($("div").width()) 

 

jQuery静态方法插件 

$.extend({ 

    randomColor:function(){ 

        return Array(6).fill(1).reduce((v,t)=>v+(~~(Math.random()*16)).toString(16),"#"); 

    } 

}) 

console.log( $.randomColor()) 

 

 

jsonp (页首) 

 

<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> 

<script> 

    // jsonp为了解决跨域,与json是没有关系,与ajax也没有关系 

    $("<div></div>").css({ 

        width:50, 

        height:50, 

        backgroundColor:"red" 

    }).appendTo("body"); 

</script> 

 

jsonp  不需要写跨域请求 

function fn(a,b){ 

    console.log(a,b) 

} 

var script=document.createElement("script"); 

script.src="http://localhost:4001?a=1&b=2&cb=fn"; 

document.body.appendChild(script); 

 

 

// 最上层 

$.getJSON(); 

$.getScript(); 

// 中间层 

$.get(); 

$.post(); 

$().load(); 

// 最底层 

$.ajax() 

 

加载JSON文件并且转换为对象 

$.getJSON("./a.json",function(data){ 

    console.log(data); 

}) 

 

加载js文件 

$.getScript("./a.js",function(script){ 

    console.log(script); 

}) 

 

jsonp通信 

$.getScript("http://localhost:4001?a=1&b=2&cb=fn"); 

function fn(a,b){ 

    console.log(a,b); 

} 

 

get方法通信 

$.get("http://localhost:4010/news?a=1&b=2",function(data){ 

    console.log(data); 

}) 

$.get("http://localhost:4010/news",{a:1,b:2},function(data){ 

    console.log(data); 

})// 等同于get方法传参,写法不同 

 

post方法通信 

$.post("http://localhost:4010/news",{a:10,b:20},function(data){ 

    console.log(data); 

}) 

 

等同于post传参 

$(document).load("http://localhost:4010/news",{a:10,b:20},function(data){ 

    console.log(data) 

}) 

 

加载页面放入某个元素中 

$("div").load("./菜单.html"); 

 

jQuery中的ajax (页首) 

$.ajax({ 

    url:"http://localhost:4010/news", 

    type:"post",//发送类型 

    data:JSON.stringify({a:1,b:2}),//发送的数据 

    success:function(data){ 

        console.log(data)//成功时执行当前函数 

    } 

}) 

 

ajax执行jsonp 

function fn(a,b){ 

    console.log(a,b) 

} 

$.ajax({ 

    url:"http://localhost:4001?a=2&b=3&cb=fn", 

    dataType:"jsonp" 

}) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值