jQuery三个版本的导入文件
//1.第一种写法
$(document).ready(function () {
})
//2.第二种写法
jQuery(document).ready(function () {
})
//3.第三种写法
$(function () {
})
//4.第四种写法
jQuery(function () {
})
//1.释放$的使用权
//注意点:释放操作必须在编写其他jQuery代码之前编写
// 释放之后就不能再使用$,改为使用jQuery
//jQuery.noConflict();
//2.自定义访问符号
var nj = jQuery.noConflict();
nj(function () {
alert("hello world")
})
//1.定义一个类
function AClass() {
}
//2.给这个类添加一个静态方法
//直接添加给类就是静态方法
AClass.staticMethod = function () {
alert("staticMethod");
}
//静态方法通过类名调用
AClass.staticMethod()
//3.给这个类添加一个实例方法
AClass.prototype.instanceMethod = function () {
alert("instanceMethod");
}
//实例方法通过类的实例调用
//创建一个实例
var a = new AClass();
//通过实例调用实例方法
a.instanceMethod();
var arr = [1, 3, 5, 7, 9];
var obj = {
0:1, 1:3, 2:5, 3:7, 4:9, length:5};
arr.forEach(function (value, index) {
console.log(value, index);
});
var arr = [1, 3, 5, 7, 9];
var obj = {
0:1, 1:3, 2:5, 3:7, 4:9, length:5};
$.each(arr, function (index,value) {
console.log(value, index);
})
$.each(obj, function (index,value) {
console.log(value, index);
})
var arr = [1, 3, 5, 7, 9];
var obj = {
0:1, 1:3, 2:5, 3:7, 4:9, length:5};
arr.map(function (value, index, array) {
console.log(value, index, array);
});
var arr = [1, 3, 5, 7, 9];
var obj = {
0:1, 1:3, 2:5, 3:7, 4:9, length:5};
$.map(arr, function (value, index) {
console.log(index, value);
});
var res1 = $.map(obj, function (value, index) {
// console.log(index, value);
return value+index;
});
var res2 = $.each(obj, function (index,value) {
// console.log(value, index);
return value+index;
});
console.log(res1);
console.log(res2);
var str = " lnj ";
var res = $.trim(str);
console.log("---"+str+"---");
console.log("---"+res+"---");
//数组
var arr = [1, 3, 5, 7, 9];
//伪数组
var arrlike = {
0:1, 1:3, 2:5, 3:7, 4:9, length:5};
//对象
var obj = {
"name":"lnj", age:"33"};
//函数
var fn = function () {
}
//window对象
var w = window;
var res = $.isWindow(w);
console.log(res);
var res = $.isFunction(jQuery);
console.log(res);
$.holdReady(true);
$(function () {
alert("ready");
});
<button>
点击按钮
button>
var btn = document.getElementsByTagName("button")[0];
btn.onclick = function () {
$.holdReady(false);
}
HTML页面
<style>
div{
width: 50px;height: 100px;background: #ff0000;margin-bottom: 50px;}
style>
<div>div>
<div>我是divdiv>
<div>他们我是div123div>
<div><span>span>div>
<div><p>p>div>
// var $div = $("div:empty");
// var $div = $("div:parent");
// var $div = $("div:contains('我是div')");
var $div = $("div:has('span')");
console.log($div);
1.什么是属性?
2.如何操作属性?
3.什么是属性节点?
4.如何操作属性节点?
5.属性和属性节点有什么区别?
// function Person() {
//
// }
// var p = new Person();
// p.name = "lnj";
// console.log(p.name);
var span = document.getElementsByTagName('span')[0];
span.setAttribute("name", "lnj");
console.log(span.getAttribute("name"));
<span name="it555">span>
$(function () {
console.log($("span").attr("class"));
$("span").attr("class", "box");
$("span").attr("abc", "123");
$("span").removeAttr("class name");
});
<body>
<span class="span1" name="it666">span>
<span class="span2" name="lnj">span>
body>
用法与attr方法类似
在操作具有true和false两个属性的属性节点,如checked,selected或者disabled使用prop(),其他使用attr()
console.log($("input").prop("checked"));// true/false
console.log($("input").attr("checked"));// checked/undefined
<input type="checkbox"/>
$(function () {
//1.给按钮添加点击事件
var btn = document.getElementsByTagName("button")[0];
btn.onclick = function () {
//2.获取输入框输入的内容
var input = document.getElementsByTagName("input")[0];
var text = input.value;
console.log(text);
//3.修改img的src属性节点的值
$("img").attr("src", text);
// $("img").prop("src", text);
}
})
<input type="text"/>
<button>切换图片button>
<br>
<img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="">
1.addClass(class|fn)
2.removeClass([class|fn])
3.toggleClass(class|fn[,sw])
<style>
*{
margin: 0;
padding: 0;
}
.class1{
width: 100px;
height: 100px;
background: red;
}
.class2{
border: 10px solid #000;
}
style>
<button>添加类button>
<button>删除类button>
<button>切换类button>
<div>div>
var btns = document.getElementsByTagName("button");
btns[0].onclick = function () {
$("div").addClass("class1");
$("div").addClass("class1 class2");
}
btns[1].onclick = function () {
$("div").removeClass("class1");
}
btns[2].onclick = function () {
$("div").toggleClass("class2");
}
1.html([val|fn])
2.text([val|fn])
3.val([val|fn|arr])
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 100px;
height: 100px;
border: 1px solid red;
}
style>
<button>设置HTMLbutton>
<button>获取HTMLbutton>
<button>设置textbutton>
<button>获取textbutton>
<button>设置valuebutton>
<button>获取valuebutton>
<div>div>
<input type="text"/>
var btns = document.getElementsByTagName("button");
btns[0].onclick = function () {
$("div").html("我是段落我是span
");
}
btns[1].onclick = function () {
console.log($("div").html());
}
btns[2].onclick = function () {
$("div").text("我是段落我是span
");
}
btns[3].onclick = function () {
console.log($("div").text());
}
btns[4].onclick = function () {
$("input").val("请输入内容");
}
btns[5].onclick = function () {
console.log($("input").val());
}
1.逐个设置CSS样式
$("div").css("width", "100px");
$("div").css("height", "100px");
$("div").css("background", "red");
2.链式设置
$("div").css("width", "100px").css("height", "100px").css("background", "blue");
3.批量设置(常用)
$("div").css({
"width": "100px",
"height": "100px",
"background": "red"
});
4.获取css样式值
console.log($("div").css("background"));
前台代码
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
border: 50px solid #000;
margin-left: 50px;
position: relative;
}
.son{
width: 100px;
height: 100px;
background: blue;
position: absolute;
left: 50px;
top: 50px;
}
style>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
border: 50px solid #000;
margin-left: 50px;
position: relative;
}
.son{
width: 100px;
height: 100px;
background: blue;
position: absolute;
left: 50px;
top: 50px;
}
style>
<div class="father">
<div class="son">div>
div>
<button>获取button>
<button>设置button>
1.获取元素的宽度
console.log($(".father").width());
2.获取元素距离窗口的偏移位
console.log($(".son").offset().left);
3.获取元素距离定位元素的偏移位
console.log($(".son").position().left);
4.设置元素属性
$(".father").width("500px");
$(".son").offset({
left: 10
});
// $(".son").position({
// left: 10
// });
$(".son").css({
left: "10px"
})
<style>
*{
margin: 0;
padding: 0;
}
.scorll{
width: 100px;
height: 200px;
border: 1px solid #000;
overflow: auto;
}
style>
<div class="scorll">
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div
div>
<button>获取button>
<button>设置button>
<br>
<br>
<br>
<br>
<br>
<br><br>
<br>
<br><br><br><br>
<br>
<br>
<br>
<br>
<br>
<br><br>
<br>
<br><br><br><br>
1.获取滚动的偏移位
console.log($(".scorll").scrollTop());
2.获取网页滚动的偏移位
注意:为了保证浏览器的兼容,获取网页滚动的偏移位需要按照如下写法
console.log($("html").scrollTop()+$("body").scrollTop());
3.设置滚动的偏移位
$(".scorll").scrollTop(300);
4.设置网页滚动的偏移位
同时保证兼容性
$("html,body").scrollTop(300);
jQuery中有两种绑定事件方式
1.eventName(fn);
编码效率略高/部分事件jQuery没有实现,所以不能添加
2.on(evenName,fn);
编码效率略低/所有js事件都可以添加
注意点:可以添加多个相同或者不同类型的事件,不会覆盖
代码示例
// $("button").click(function () {
// alert("hello lnj");
// });
// $("button").click(function () {
// alert("hello lnj2");
// });
// $("button").mouseleave(function () {
// alert("hello mouseleave");
// });
// $("button").mouseenter(function () {
// alert("hello mouseenter");
// });
$("button").on("click", function () {
alert("hello on lnj1")
})
$("button").on("click", function () {
alert("hello on lnj2")
})
<button>我是按钮button>
function test1() {
alert("hello test1");
}
function test2() {
alert("hello test2");
}
$("button").click(test1);
$("button").click(test2);
$("button").mouseleave(function () {
alert("hello mouseleave");
});
$("button").mouseenter(function () {
alert("hello mouseenter");
});
//off方法如果不传递参数,会移除所有的事件
$("button").off();
//off方法如果传递一个参数,会移除所有指定类型的事件
$("button").off("click");
//off方法如果传递2个参数,会移除所有指定类型的指定事件
$("button").off("click");
<button>我是按钮button>
前台代码
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
style>
<div class="father">
<div class="son">
div>
div>
<a href="www.baodu.com">我是百度a>
<form action="www.taobao.com">
<input type="text">
<input type="submit">
form>
1.什么是时间冒泡?
事件发生后,浏览器通常首先触发事件发生元素上的事件处理程序,然后是它的父元素,父元素的父元素……依此类推, 直到文档的根元素为止。
2.什么是默认行为?
DOM元素本身具有一定的行为方式,比如form表单就有提交方式。
3.如何阻止事件冒泡?
使用stopPropagation()或者return false
$(".son").click(function (event) {
alert("son");
// return false;
event.stopPropagation();
});
$(".father").click(function () {
alert("father");
});
4.如何阻止默认行为?
使用preventDefault()或者return false
$("a").click(function (event) {
alert("弹出注册框");
return false;
event.preventDefault();
});
前台代码
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
style>
<div class="father">
<div class="son">
div>
div>
<a href="http://www.baidu.com"><span>我是百度span>a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
form>
1.trigger
面试问题:a标签无法直接触发默认行为,需要使用span标签
$(".son").click(function (event) {
alert("son");
});
$(".father").click(function () {
alert("father");
});
// $(".son").trigger("click");
// $(".son").triggerHandler("click"); //$("input[type='submit']").trigger("click");
// $("a").click(function () {
// alert("a");
// });
// $("a").trigger("click");
$("span").click(function () {
alert("a");
});
$("span").trigger("click");
前台代码
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
style>
<div class="father">
<div class="son">
div>
div>
想要自定义事件,必须满足两个条件
$(function () {
$(".son").on("myClick", function () {
alert("son");
});
$(".son").trigger("myClick");
})
想要事件的命名空间有效,必须满足两个条件
$(function () {
$(".son").on("click.zs", function () {
alert("son1");
});
$(".son").on("click.ls", function () {
alert("son2");
});
$(".son").trigger("click.zs ");
})
1.利用trigger触发子元素带命名空间的事件,那么父元素带相同命名空间的事件也会被触发,而父元素没有命名空间的事件不会被触发。
2.利用trigger触发子元素不带命名空间的事件,那么子元素所有相同类型的事件和父元素所有相同类型的事件都会被触发。
$(".father").on("click.ls", function () {
alert("father click1");
});
$(".father").on("click", function () {
alert("father click2");
});
$(".son").on("click.ls", function () {
alert("son click1");
});
// $(".son").trigger("click.ls");
$(".son").trigger("click.");
前台代码
<ul>
<li>我是第一个lili>
<li>我是第二个lili>
<li>我是第三个lili>
ul>
<button>新增一个libutton>
1.什么是事件委托?
请别人帮忙做事情,然后将结果反馈给我们。
2.在jQuery中,如果通过核心函数找到的元素不止一个,那么在添加事件的时候。
3.jQuery会遍历所有找到的元素,给所有找到的元素添加事件。
$("button").click(function () {
$("ul").append("我是新增的li ")
});
$("ul").delegate("li", "click", function () {
console.log($(this).html())
})