PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > jQuery基本语法--2019-05-21

jQuery基本语法--2019-05-21

小人物的博客
小人物的博客 原创
2019年05月21日 18:42:33 1139浏览

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery语法基础 0520作业</title>
</head>
<body>
<ul>
    <li id="li1">item1</li>
    <li id="li2">item2</li>
    <li class="lis">item3</li>
    <li class="lis">item4</li>
    <li id="li3">item5</li>
</ul>


<script src="static/js/jquery-3.4.1.js"> </script>
<script>
    // DOM对象与jQuery对象之间的互相转换
    // 1. $(): 将DOM对象转为jQuery对象
    $('ul li:nth-of-type(3)')
        .css('color','red');


    // 2.将jQuery对象转为DOM对象
    var lis = $('li');
    console.log(lis);
    lis[4].innerHTML='选择了item5';

    //常用选择器
    //id选择器
    $('#li1').css('background','lightgreen');
    //类选择器
    $('.lis').css('background','lightblue');
    //全部选择
    $('*').css('font-size','120%')
    //获取第一个元素
    $('li:first').css('color','yellow')
</script>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

jQuery基本语法:

  1. $() 基本用法

    * 选择元素
    * 创建元素
    * 包装元素
    * 执行回调

  2. DOM对象与jQuery对象的相互转换

    * DOM转jQuery: $()
    * jQuery转DOM: $()[], $().get()

  3. 常用选择器

    ### 3.1 基本选择器

    * `#id`: ID选择器
    * `element`: 元素/标签选择器
    * `.class`: 类/class选择器
    * `*`: 通配选择器
    * `selector1,selector2,selectorN`: 群组选择器

    ### 3.2 层级选择器

    * `ancestor descendant`: 后代选择器, 中间用空格分隔
    * `parent > child`: 子选择器
    * `prev + next`: 相邻下一个兄弟元素
    * `prev ~ siblings`: 相邻后面的所有兄弟元素

    ### 3.3 基本筛选器

    * `:first`: 获取第一个元素
    * `:not(selector)`: 从集合中去掉指定的元素
    * `:even`: 匹配集合中索引为偶数的元素(从0开始)
    * `:odd`: 匹配集合中索引为奇数的元素(从0开始)
    * `:eq(index)`: 匹配指定索引的元素
    * `:gt(index)`: 匹配所有大于指定索引的元素
    * `:lang`: 匹配指定语言的元素
    * `:last`: 匹配最后一个元素
    * `:lt(index)`: 匹配所有小于指定索引的元素
    * `:header`: 匹配类似`<h1>,<h2>,<h3>...`标题元素
    * `:animated`: 匹配所有正在执行动画的元素
    * `:focus`: 匹配当前获取焦点的元素
    * `:root`: 获取当前文档根元素,html文档永远是`<html>`

    ### 3.4 内容筛选器

    * `:contains(text)`: 返回包含指定文本的元素
    * `:empty`: 返回不包含任何子元素或文本的元素
    * `:has(selector)`: 返回包含有指定元素(由选择器设置)的元素
    * `:parent`: 匹配含有子元素或文本的元素(非空元素)

    ### 3.5 可见性选择器

    * `:hidden`: 获取所有不见的元素或`type="hidden"`的元素
    * `:visible`: 匹配所有可见元素

    ### 3.6 属性选择器

    * `[attribute]`: 匹配包含指定属性的元素,如`$('p[id]')`
    * `[attribute=value]`: 匹配属性等于指定值的元素,`$('p[id="news"]')`
    * `[attribute!=value]`:匹配属性不等于指定值的元素,`$('p[id!="news"]')`
    * `[attribute^=value]`: 匹配属性以指定文本开始的元素,`$('p[id^="pre"]')`
    * `[attribute$=value]`: 匹配属性以指定文本结束的元素,`$('p[id$="pre"]')`
    * `[attribute*=value]`: 匹配属性包含指定文本的元素,`$('p[id*="pre"]')`
    * `[attrSel1][attrSel2][attrSelN]`:匹配同时满足多个属性选择器的元素,是前面的复合

    ### 3.7 子元素选择器

    * `:first-child`: 匹配指定元素的第一个子元素, 如`$('ul :first-child')`
     * 注: 与`:first`不同, 可能会匹配到多个父级的第一个子元素,类似`:nth-child(1)`
    * `:first-of-type`: 与上面的类似,但多个元素的类型限制,`$('div p:first-of-type')`
    * `:last-child`: 匹配指定元素的最后一个子元素, 如`$('ul :last-child')`
    * `:last-of-type`:与上面的类似,但多个元素的类型限制,`$('div p:last-of-type')`
    * `:nth-child()`: 匹配父元素下指定索引的元素(从1开始),`$('ul :nth-child(2)')`
    * `:nth-last-child()`: 匹配父元素下倒数索引的元素, `$('ul :nth-last-child(3)')`
    * `:nth-last-of-type()`: 与前一个相比,多了元素类型限制
    * `:nth-of-type()`: 功能与`:nth-child()`类似,但多了元素类型限制,`:`前加选择器名
    * `:only-child`: 匹配父元素下唯一子元素, `$('div :only-child')`
    * `only-of-type`: 匹配父元素下唯一指定类型的子元素,`$('div p:only-of-type')`

    ### 3.7 表单选择器

    #### 3.7.1 控件类型选择器

    > 选择器名称与控件中的`type`属性值对应, 如`type="text"`,就用`:text`表示

    * `:input`: 匹配`<input>, <textarea>, <select>, <button>`元素
    * `:text`: 匹配`<input type="text">`的元素, 即所有单行文本框
    * `:password`: 匹配所有密码框元素,`<input type="password">`
    * `:radio`:  匹配所有单选按钮元素,`<input type="radio">`
    * `:checkbox`: 匹配所有复选框元素, `<input type="checkbox">`
    * `:submit`: 匹配提交按钮,因为`<button>`默认`type="submit"`,所以总会匹配
    * `:image`: 匹配所有图像域控件`<input type="image">`
    * `:reset`: 返回重置按钮控件`<input type="reset">`
    * `:button`: 返回所有按钮控件`<input type="button">`
    * `:file`: 返回所有的文件上传域控件`<input type="file">`
    * `:hidden`: 返回隐藏域控制`<input type="hidden">`

    > **特别提示:**

    * 该选择器是jQuery独有,非CSS规范,无法利用原生DOM的性能优势
    * 属于过时的选择器, 推荐使用`[type="类型"]`替代
    * 如果仍想使用该选择器, 推荐使用`.filter(':input/:text...')`代替

    ### 3.7.2 控件状态选择器

    * `:enabled`: 匹配表单中所有有效的元素(无`disabled`属性)
    * `:disabled`: 匹配所有禁用的元素
    * `:checked`: 匹配复选框,单选框中的被选中的元素(具有`checked`属性的元素)
    * `:selected`: 匹配下拉列表中被选中的元素(具有`selected`属性的元素)

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议