整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

一文详解javascript jQuery库,极大简化网页交互编程

么是jQuery?

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");
});

元素的隐藏、展现

  • $box.hide():隐藏,可以添加参数,表示消失时间
  • $box.show():展现,可以添加参数,表示出现时间
  • $box.fadeOut():慢慢消失,可以添加参数,表示消失时间
  • $box.fadeIn():慢慢出现,可以添加参数,表示出现时间

常用选择器

  • $('#myId'):选择id为myId的标签
  • $('.myClass'):选择class为myClass的标签
  • $('div'):选择所有的div标签
  • $("a[target='_blank']"):选取所有target属性值等于"_blank"的<a>元素

项目目标:点击叉号,关闭弹窗。

$(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>

选择集转移

  • $('#box').prev(); 表示选择id是box元素的上一个的同级元素
  • $('#box').prevAll(); 表示选择id是box元素的上面所有的同级元素
  • $('#box').next(); 表示选择id是box元素的下一个的同级元素
  • $('#box').nextAll(); 表示选择id是box元素的下面所有的同级元素
  • $('#box').parent(); 表示选择id是box元素的父元素
  • $('#box').children(); 表示选择id是box元素的所有子元素
  • $('#box').siblings(); 表示选择id是box元素的其它同级元素
  • $('#box').find('.myClass'); 表示选择id是box后代元素的class等于myClass的元素
<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>

对内容、文本操作

  • $box.html():获取、修改HTML内容
  • $box.text():获取、修改文本信息
  • $box.val():获取、修改文本的值
<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>

对类的操作

  • $box.addClass(class_name):添加某个类
  • $box.removeClass(class_name):删除某个类
  • $box.hasClass(class_name):判断某个类是否存在
<div></div>

<script>
    $(function(){
        let $div = $("div");
        console.log($div);
        $div.on("click", function(e){
            $(this).addClass("active");
            console.log($(this).hasClass("active"));
        });
    });
</script>

相关推荐

  • Javascript对象和选项卡实现
  • 一文详解javascript轮播图
  • Javascript简介和基础数据类型
  • 一文详解Javascript定时器
  • 一文详解Javascript DOM树结构
  • 一文详解Javascript鼠标事件,拖拽原理
  • Javascript点击按钮控制图片切换
  • 一文详解javascript函数和面向对象编程

视频讲解

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>