JQuery下载和一些语法

最近学了六种jQuery选择器,我想把学过案例和知识结合起来,给大家分享下!

那么既然学jQuery选择器,肯定要先了解下jQuery是什么吧!jQuery是一个快速、简洁的JavaScript框架,相当于用jQuery能更加高效的创建JS代码,这就是jQuery,选择器是它的一种语法,代码的使用上就不能再用JS语法了,要用jQuery创建的语法。

那么用它,肯定要进行下载吧!下面附上链接和官网图片jQueryhttps://jquery.com/

1.基本选择器

这种选择器学起来简单好理解,但也很重要,因为后面的选择器是依托它的!所以这个必须学牢固

下面是基本选择器的代码:

复制代码
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-3.2.1.js"></script>
    (这是调用jQuery包,只有调用后才能使用以下的jQuery语句)
</head>
<body>
    <div id="one" style="width: 500px;height: 150px;border: 1px dashed;"></div>
    <div id="two" style="width: 500px;height: 150px;border: 1px dashed;"></div>
    <div id="three" style="width: 500px;height: 150px;border: 1px dashed;"></div>
    <div class="four" style="width: 500px;height: 150px;border: 1px dashed;"></div>
    <script>
        //(以下是基本选择器)
        //id选择器
        console.log($("#one"));
        //标签名选择器
        console.log($("div"));
        //class选择器
        console.log($(".four"));
        //*选择器
        console.log($("*"));
        //联合选择器
        $(function (){
            console.log($("#one,#two,.four"));
        })
    </script>
</body>
</html>

结合代码看控制台,对应的个数分别是1、4、1、10、3,通过这些条件能进行快速、准确的查询。很方便!

复制代码
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-3.2.1.js"></script>
</head>
<body>
    <div id="dv" style="width: 300px;height: 300px;border: 2px solid;">
      <input id="a" type="button" value="按钮1"/>
      <div style="width: 100px;height: 100px;border: 1px solid;padding: 10px;">
        <input id="c" type="button" value="按钮2"/>
      </div>
      <input class="b" type="button" value="按钮3"/>
      <input type="button" value="按钮4"/>
    </div>
    <button type="button" οnclick="text1()">验证后代元素选择器</button>
    <button type="button" οnclick="text2()">验证子元素选择器</button>
    <button type="button" οnclick="text3()">验证紧邻同辈元素选择器</button>
    <button type="button" οnclick="text4()">验证同辈元素选择器</button>
    <script>
    function text1(){
      console.log($("#dv input"));
    }
    function text2(){
      console.log($("#dv>input"));
    }
    function text3(){
      console.log($(".b+input"));
    }
    function text4(){
      console.log($("#a~input"));
    }
  </script>
</body>
</html>

1.选择器之间是空格的作用是验证后代元素

2.选择器之间是'>'的作用是验证子元素,就是只查看自己的儿子

3.选择器之间是'+'的作用是验证紧邻的同辈元素(不能是大哥)

4.选择器之间是'~'的作用是验证相邻的同辈元素(不能是大哥)

相关推荐
We་ct3 分钟前
React Scheduler & Lane 详解
前端·react.js·前端框架·reactjs·个人开发·任务调度·优先
小道士写程序17 分钟前
Babylon.js WebGPU Ocean Demo — 完整踩坑记录
开发语言·javascript·ecmascript
kgduu19 分钟前
js之json处理
前端·javascript·json
@木尘32 分钟前
前端面试【 八股文篇】
前端·面试·职场和发展
吴佳浩39 分钟前
OpenClaw、Claude Code 等 Agent 为什么都选择 Node.js?
前端·人工智能·langchain
小小小小宇42 分钟前
React 19 useActionState 深度解析 & Vue 2.7 移植实战
前端
远山枫谷1 小时前
Vue2 vs Vue3 全面对比(含代码示例+迁移指南)
前端·vue.js
z止于至善1 小时前
服务器发送事件(SSE):前端实时通信的轻量解决方案
前端·web·服务器通信
小小小小宇1 小时前
React useState 深度源码原理解析
前端
前端小棒槌1 小时前
TypeScript 核心知识点
前端