前端学习-jQuery

老师博客:https://www.cnblogs.com/yuanchenqi/articles/6070667.html

day43,day44

jquery 中文文档:http://jquery.cuishifeng.cn/

首先我们得下载一个jquery文件 jquery-3.1.1.js,然后工程引入

 

<script src="jquery-3.1.1.js"></script>

jquery的基础语法:$(selector).action()

selector 是选择器

 

jQuery 是什么

<1>jQuery 由 John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team。

<2>jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE!

<3>它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器

<4>jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。

<5>jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择

什么是jQuery对象:
jQuery 对象就是通过jQuery包装DOM对象后产生的对象。jQuery 对象是 jQuery 独有的如果一个对象是 jQuery 对象那么它就可以使用 jQuery 里的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="jquery-3.1.1.js"></script>
 
<div class="div1">hello world</div>
<script>
    $(".div1").css("color","red")
</script>
</body>
</html>

寻找元素(选择器和筛选器) 

3.1 选择器

3.1.1 基本选择器

3.1.2 层级选择器

3.1.3 基本筛选器

3.1.4 属性选择器

3.1.5 表单选择器

$("[type='text']")----->$(":text")         注意只适用于input标签  : $("input:checked")

  

查找筛选器*****

 

 

$("ul li").hasclass("test")  判断某一个标签内是不是含有class=test的筛选器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="div1"></div>
 
<script src="jquery-3.1.1.js"></script>
<script>
    console.log($("div").hasClass("div1"))
</script>
</body>
</html>

  

实例:左侧菜单

实现点击母菜单按钮,罗列子菜单,其他菜单隐藏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .outer{
            width: 100%;
            height: 100%;
 
        }
        .menu{
            float: left;
            background-color: #2f4050;
            /*background-color: white;*/
            width: 15%;
            height: 1000px;
            font-size: 13px;
            color: whitesmoke;
 
        }
        .content{
            float: left;
            background-color: antiquewhite;
            width: 83%;
            height: 1000px;
            margin-left: 10px;
        }
        .title{
            background-color: yellowgreen;
            line-height: 30px;
            text-align: center;
        }
        .hide{
            display: none;
        }
    </style>
 
</head>
<body>
<script src="jquery-3.1.1.js"></script>
 
<div class="outer">
    <div class="menu">
        <div class="item">
            <div class="title" onclick="show(this)">用户管理</div> <!-- onclick 绑定的函数 传参数 关键字this,点击后直接获取到本次点击的对象,然后传给show函数 -->
            <div class="con">
                <div>查看用户组</div>
                <div>查看用户</div>
            </div>
        </div>
        <div class="item ">
            <div class="title" onclick="show(this)">资产管理</div>
            <div class="con hide">
                <div>查看资产组</div>
                <div>查看资产</div>
                <div>查看机房</div>
            </div>
        </div>
        <div class="item">
            <div class="title" onclick="show(this)">授权管理</div>
            <div class="con hide">
                <div>Sudo</div>
                <div>系统用户</div>
                <div>授权</div>
            </div>
        </div>
        <div class="item ">
            <div class="title" onclick="show(this)">上传下载</div>
            <div class="con hide">
                <div>文件上传</div>
                <div>文件下载</div>
            </div>
        </div>
    </div>
    <div class="content"></div>
 
    <script>
        function show(self) {  // 这里的self 就是点击的标签对象,我们要取消点击标签的hide,其他标签添加hide属性,我们先找到我们点击标签的parent,然后利用parent的siblings,再找到他们的孩子的con标签
                $(self).next().removeClass("hide"); //
                $(self).parent().siblings().children(".con").addClass("hide"); //这里有hide就不添加,没hide就添加
        }
    </script>
</div>
</body>
</html>

  

 

posted @   BigBao的博客  阅读(290)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示