jQuery和JavaScript的作用一样,都是负责网页行为操作,增加网页和用户的交互效果的,而且jQuery简化了JavaScript编程,jQuery实现交互效果更简单。
jQuery也兼容了现在主流的浏览器,增加了程序员的开发效率。
可以通过jQuery官网获取库文件:
引入jquery.js文件:
<script src="/static/js/jquery-3.6.3.min.js"></script>jQuery入口函数:(速度比window.onload更快)
<script>
$(document).ready(function(){
alert("hello jQuery");
});
</script>简写:
$(function(){
alert("Hello jQuery");
});项目目标:点击叉号,关闭弹窗。
$(function(){
let $container = $("#container");
// console.log($container);
let $btn = $("#btn");
$btn.click(function(){
$container.css({'display': 'none'});
});
});<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
</ul>
<script>
$(function(){
$("li:first").css({"color": "red"});
$("li:eq(1)").css({"color": "blue"});
$("li:odd").css({"color": "pink"});
$("li:even").css({"color": "green"});
});
</script><ul>
<li>1</li>
<li>2</li>
<li>3</li>
<ul id="list">
<li class="box1">4-1</li>
<li class="box1">4-2</li>
<ul>
<li class="box1">4-3-1</li>
<li class="box2">4-3-2</li>
<li class="box2">4-3-3</li>
</ul>
</ul>
<li>5</li>
<li>6</li>
</ul>
<script>
$(function(){
// $("#list").prev().css({'color':'red'});
// $("#list").prevAll().css({'color':'red'});
// $("#list").next().css({'color':'red'});
// $("#list").nextAll().css({'color':'red'});
// $("#list").children(".box1").css({'color':'red'});
// $("#list").siblings().css({'color':'red'});
$("#list").find(".box1").css({'color':'red'});
});
</script><div id="box">hello</div>
<input type="text" name="" id="" value="100">
<a href="" id="mylink">黑猫编程</a>
<script>
let $box = $("#box");
console.log($box.html());
console.log($box.text());
$box.html("<span style='color:red'>你好</span>");
$box.append("<span style='color:green'>你好</span>");
let $input =$("input");
$input.val("200");
let $a = $("a");
$a.prop({"href":"https://noi.hioier.com/", "target":"_blank"});
</script><div id="box1">
<div id="box2">
</div>
</div>
<button>停止</button>
<script>
$("#box1").on("click", function(e){
console.log("click box1");
});
$("#box2").on("click", function(e){
console.log("click box2");
return false; // 阻止事件冒泡
});
$("button").on("click", function(e){
$("#box1").off("click");
$("#box2").off("click");
});
</script><div></div>
<script>
$(function(){
let $div = $("div");
console.log($div);
$div.on("click", function(e){
$(this).addClass("active");
console.log($(this).hasClass("active"));
});
});
</script>ab选项卡切换效果应用到很多网页中,今天总结了一个超简单的切换效果,相信你一看就能学会。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://www.w3school.com.cn//jquery/jquery-1.11.1.min.js"></script>
<title>tab选项卡切换</title>
<style>
* {
list-style: none;
}
#outer {
width: 450px;
margin: 150px auto;
background: #fff;
}
#tab {
width: 409px;
border: #ccc solid 1px;
margin: 50px auto 0 auto;
}
#tab li {
float: left;
color: #7c4b04;
height: 30px;
cursor: pointer;
line-height: 30px;
padding: 0 20px;
}
#tab li.current {
color: #fff;
background: blue;
}
#content {
border: 1px solid #ccc;
border-top-width: 0;
height: 223px;
}
#content ul {
line-height: 25px;
display: none;
margin: 0 30px;
padding: 40px 0;
}
#content ul li {
line-height: 28px;
}
#content ul li a {
cursor: pointer;
}
</style>
</head>
<body>
<div id="outer">
<ul id="tab">
<li class="current">今日热搜</li>
<li>体育新闻</li>
<li>css3</li>
</ul>
<div id="content">
<ul style="display: block;">
<li>
<a>女子网上晒猫被泸州老窖索赔20万</a>
</li>
<li>
<a>韩国丈夫陪妻子打疫苗,得知资金也可以打后,瞬间激动得满眼放光</a>
</li>
<li>美国出现“招魂女孩”,现场表演异能,评委吓得逃走了</li>
</ul>
<ul>
<li>
<a>CBA三分王耍大牌</a>
</li>
<li>
<a>欧协杯附件赛传捷报,穆里尼奥罗马首秀获胜</a>
</li>
<li>
<a>096期体彩大乐透晒票,看着开奖结果,彩民笑了</a>
</li>
</ul>
<ul>
<li>tab3</li>
</ul>
</div>
</div>
<script>
$(function () {
$("#tab li").mouseover(function () {
var index = $(this).index();
$(this).addClass('current').siblings().removeClass('current');
$("#content ul").eq(index).css("display", "block").siblings().css("display", "none")
})
})
</script>
</body>
</html>
定义动画的效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>47-jQuery自定义动画效果</title>
<style type="text/css">
*{
margin: 0; /*外边距:0*/
padding: 0; /*内边距:0*/
}
div{
width: 100px; /*宽:100*/
height: 100px; /*高:100*/
background: red; /*背景:红色*/
margin-top: 20px; /*外上边距:20*/
}
.two{
background: blue; /*背景:蓝色*/
}
</style>
<script src="../static/js/jquery-3.6.0.js"></script>
<script>
$(function(){
$("button").eq(0).click(function(){ // 第一个按钮的点击事件
// 红色div的左边距用3秒时间移动到500像素,完成后弹窗
$(".one").animate({marginLeft: 500}, 3000, function(){alert('自定义动画执行完毕')})
// 蓝色div的左边距用3秒时间移动到500像素,完成后弹窗
$(".two").animate({marginLeft: 500}, 3000, 'linear', function(){alert('自定义动画执行完毕')})
})
// 累加属性的点击事件
$('button').eq(1).click(function(){
// 红色div的宽度用1秒时间增加100像素,完成后弹窗
$('.one').animate({width:'+=100'}, 1000, function(){alert('累加动画执行完毕')})
})
})
</script>
</head>
<body>
<button>操作属性</button>
<button>累加属性</button>
<div class="one"></div>
<div class="two"></div>
</body>
</html>
*请认真填写需求信息,我们会在24小时内与您取得联系。