1、初识,一个简单的演示
jQuery核心函数:
/
j
Q
u
e
r
y
j
Q
u
e
r
y
核
心
对
象
:
执
行
/jQuery jQuery核心对象:执行
/jQueryjQuery核心对象:执行()返回的对象
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
>
<script type
="text/javascript">
jQuery(function(){
$("#but2").click(function(){
var username
=$(".username").val()
console
.log(username
);
})
})
</script
>
<head
>
<title
>testScript
</title
>
</head
>
<body
>
用户名:
<input type
="text" class="username">
<button id
="but1">确定(原生版)
</button
>
<button id
="but2">确定(jQuery版)
</button
>
</body
>
</html
>
打印输入的文本
更清晰的写法
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
><!-- 引入jquery库
-->
<script type
="text/javascript">
jQuery(function(){
var $btn2
=$("#but2")
$btn2
.click(function(){
var username
=$(".username").val()
console
.log(username
);
})
})
</script
>
<head
>
<title
>testScript
</title
>
</head
>
<body
>
用户名:
<input type
="text" class="username">
<button id
="but1">确定(原生版)
</button
>
<button id
="but2">确定(jQuery版)
</button
>
</body
>
</html
>
2、添加一个新的html元素
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
><!-- 引入jquery库
-->
<script type
="text/javascript">
$(function(){
$("#but1").click(function(){
$("<input type='text' name='newtext'/><br/>").appendTo($("#mydiv"));
})
});
var myarray
=["hello "," world "," ha ppy"];
$
.each(myarray
,function(index
,item
){
console
.log(index
+"--"+$
.trim(item
));
})
var str
=" happy birtday ";
console
.log("---"+str
.trim()+"---");
</script
>
<head
>
<title
>testScript
</title
>
</head
>
<body
>
<div id
="mydiv">
<button id
="but1">测试
</button
> <br
/>
<input type
="text" id
="text1"/><br
/>
<input type
="text" id
="text2"/><br
/>
</div
>
</body
>
</html
>
打印: 0–hello 1–world 2–ha ppy —happy birtday—
3、jQuery中对应原生JavaScript的onload写法(入口函数的写法)
原生写法:
window
.onload
=funciton (event
){
}
jQuery写法:
$(document
).ready(function (){
.....
});
$(function (){
.....
});
4、通过jQuery获取img元素的宽高
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
><!-- 引入jquery库
-->
<script type
="text/javascript">
$(document
).ready(function (){
var $img
=$("img")[0];
console
.log($img
);
var $width
=$img
.width
;
console
.log($width
);
});
</script
>
<head
>
<title
>testScript
</title
>
</head
>
<body
>
<div id
="mydiv">
<ul
>
<li
><a href
="#">我是谁
</a
></li
>
<li
><a href
="#">我到底是谁
</a
></li
>
<li
><a
>我才不管你是谁
</a
></li
>
<li
><img src
="images/img2.PNG"/></li
>
</ul
>
</div
>
</body
>
</html
>
打印:90
5、添加一个新的子标签
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
><!-- 引入jquery库
-->
<script type
="text/javascript">
$(function (){
var $mydiv
=$("#mydiv");
var $myp
=$("<P>这里有一个新的段落</P>");
$mydiv
.append($myp
);
var span
=document
.getElementById("myspan");
console
.log(span
);
var $myspan
=$(span
);
console
.log($myspan
);
});
</script
>
<head
>
<title
>testScript
</title
>
</head
>
<body
>
<div id
="mydiv">
</div
>
</body
>
</html
>
6、原生方法调用类方法和实例方法
<script type
="text/javascript">
function NormalClass(){
}
NormalClass
.staticMethod=function (){
console
.log("this is a static method");
}
NormalClass
.staticMethod();
NormalClass
.prototype
.instanceMethod=function(){
console
.log("this is a instance method");
}
var newInstance
=new NormalClass();
newInstance
.instanceMethod();
</script
>
打印: this is a static method this is a instance method
7、原生方法遍历数组,jQuery方法遍历数组和伪数组对象
<script type
="text/javascript">
var myarray
=[3,44,6,8,9,4];
var obj
={0:23,1:44,2:56,3:454,length
:4}
myarray
.forEach(function(value
,index
){
console
.log(index
+":"+value
);
})
console
.log("------------");
$(function (){
$
.each(myarray
,function (value
,index
){
console
.log(index
+":"+value
);
})
console
.log("------------");
$
.each(obj
,function(value
, index
){
console
.log(index
+":"+value
);
});
});
</script
>
8、原生map方法遍历数组,jQuery的map方法遍历数组和伪数组对象
<script type
="text/javascript">
var myarray
=[3,44,6,8,9,4];
var obj
={0:23,1:44,2:56,3:454,length
:4}
myarray
.map(function(value
,index
,array
){
console
.log(index
+":"+value
+"--"+array
);
})
console
.log("----");
$(function (){
$
.map(myarray
,function(value
,index
){
console
.log(index
+":"+value
);
})
console
.log("----");
var newmap
=$
.map(obj
,function(value
,index
){
console
.log(index
+":"+value
);
return index
+value
;
})
console
.log("===");
console
.log(newmap
);
});
</script
>
9、jQuery入口函数的挂起和解除挂起
<html
>
<style type
="text/css">
</style
>
<script language
="javascript" src
="jquery-3.5.1.js"></script
><!-- 引入jquery库
-->
<script type
="text/javascript">
$
.holdReady(true);
$(function (){
console
.log("入口函数准备就绪");
});
</script
>
<head
>
</head
>
<body
>
<button
>点击
</button
>
<script type
="text/javascript">
var mybut
=document
.getElementsByTagName("button")[0];
mybut
.onclick=function(){
alert("here is to pasue");
$
.holdReady(false);
}
</script
>
</body
>
</html
>
10、empty、parent、contains、has选择器
<script type
="text/javascript">
$(function (){
$("div:empty").css({
backgroundColor
:"red"
});
$("div:parent").css({
backgroundColor
:"blue"
});
$("div:contains('我的内容显示')").css({
backgroundColor
:"yellow"
})
$("div:has('span')").css({
backgroundColor
:"pink"
})
});
</script
>