文章目录 1. 基本介绍 2.原理示意图 3.快速入门 1.下载jQuery 2.创建文件夹,放入jQuery 3.引入jQuery 4.代码实例 4.jQuery对象与DOM对象转换 1.基本介绍 2.dom对象转换JQuery对象 3.JQuery对象转换dom对象 4.jQuery对象获取数据 获取value使用val() 获取内容使用text() 5.jQuery选择器 1.基本介绍 2.基本选择器 3.层级选择器 4.基础过滤选择器 5.内容过滤选择器 6.可见度过滤选择器 7.属性过滤选择器 8.子元素过滤选择器 9.表单属性过滤选择器 10.表单选择器 11.常用选择器 **一般使用父子/基本选择器 —> 属性选择器 —> 过滤即可解决大多数情况** 1.基本选择器 2.可见度过滤选择器 3.子元素过滤选择器 4.属性过滤选择器 5.表单属性过滤选择器 6.表单选择器 6.作业
1. 基本介绍
2.原理示意图
3.快速入门
1.下载jQuery
2.创建文件夹,放入jQuery
3.引入jQuery
4.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> Title</ title> < script type = " text/javascript" src = " jquery/jquery-3.6.0.min.js" > </ script> < script> $ ( function ( ) { var $btn01 = $ ( "#btn01" ) ; $btn01. click ( function ( ) { alert ( "hello,jquery.." ) } ) } ) </ script>
</ head>
< body>
< button id = " btn01" > 按钮</ button>
</ body>
</ html>
4.jQuery对象与DOM对象转换
1.基本介绍
2.dom对象转换JQuery对象
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> Title</ title> < script type = " text/javascript" src = " jquery/jquery-3.6.0.min.js" > </ script> < script> window. onload = function ( ) { var elementById = document. getElementById ( "username" ) ; var $val = $ ( elementById) . val ( ) ; alert ( $val) ; } </ script>
</ head>
< body>
用户名:< input type = " text" id = " username" name = " username" value = " 孙显圣" >
</ body>
</ html>
3.JQuery对象转换dom对象
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> Title</ title> < script type = " text/javascript" src = " jquery/jquery-3.6.0.min.js" > </ script> < script> window. onload = function ( ) { var $username = $ ( "#username" ) ; var username1 = $username[ 0 ] ; var username2 = $username. get ( 0 ) ; alert ( username2. value + "1" ) alert ( username1. value + "2" ) } </ script>
</ head>
< body>
用户名:< input type = " text" id = " username" name = " username" value = " 孙显圣" >
</ body>
</ html>
4.jQuery对象获取数据
获取value使用val()
获取内容使用text()
5.jQuery选择器
1.基本介绍
2.基本选择器
1.基本介绍
2. 代码实例
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 基本选择器应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 60px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } </ style> < script type = " text/javascript" src = " jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) {
$ ( "#b1" ) . click ( function ( ) { $ ( "#one" ) . css ( "background" , "#0000FF" ) } )
$ ( "#b2" ) . click ( function ( ) { $ ( ".mini" ) . css ( "background" , "#FF0033" ) } )
$ ( "#b3" ) . click ( function ( ) { $ ( "div" ) . css ( "background" , "#00FFFF" ) } )
$ ( "#b4" ) . click ( function ( ) { $ ( "*" ) . css ( "background" , "#00FF33" ) } )
$ ( "#b5" ) . click ( function ( ) { $ ( "span, #two, .mini " ) . css ( "background" , "#3399FF" ) } ) } ) </ script>
</ head>
< body>
< input type = " button" value = " 改变 id 为 one 的元素的背景色为 #0000FF" id = " b1" />
< input type = " button" value = " 改变 class 为 mini 的所有元素的背景色为 #FF0033" id = " b2" />
< input type = " button" value = " 改 变 元 素 名 为 <div> 的 所 有 元 素 的 背 景 色 为 #00FFFF" id = " b3" />
< input type = " button" value = " 改变所有元素的背景色为 #00FF33" id = " b4" />
韩顺平 Java 工程师
< input type = " button" value = " 改变所有的<span>元素和 id 为 two class 为 .mini 的元素的背
景色为 #3399FF" id = " b5" />
< hr/>
< div id = " one" class = " mini" > div id 为 one</ div>
< div id = " two" > div id 为 two</ div>
< div id = " three" class = " mini" > div id 为 three</ div>
< span id = " s_one" class = " mini" > span one</ span>
< span id = " s_two" > span two</ span>
</ body>
</ html>
3.层级选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 层次选择器应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 80px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } </ style> < script type = " text/javascript" src = " jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "div" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b2" ) . click ( function ( ) { $ ( "body > div" ) . css ( "background" , "#FF0033" ) } ) $ ( "#b3" ) . click ( function ( ) { $ ( "#one + div" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b4" ) . click ( function ( ) { $ ( "#two ~ div" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b5" ) . click ( function ( ) { $ ( "#two" ) . siblings ( "div" ) . css ( "background" , "#0000FF" ) } ) } ) </ script>
</ head>
< body>
< input type = " button" value = " 改变 <body> 内所有 <div> 的背景色为 #0000FF" id = " b1" />
< input type = " button" value = " 改变 <body> 内子 <div> 的背景色为 #FF0033" id = " b2" />
< input type = " button" value = " 改变 id 为 one 的下一个 <div> 的背景色为 #0000FF" id = " b3" />
< input type = " button" value = " 改变 id 为 two 的元素后面的所有兄弟<div>的元素的背景色为 # #0000FF" id = " b4" />
< input type = " button" value = " 改变 id 为 two 的元素所有 <div> 兄弟元素的背景色为 #0000FF" id = " b5" />
< hr/>
< div id = " one" class = " mini" > div id为one
</ div>
< div id = " two" > div id为two< div id = " two01" > id two01</ div> < div id = " two02" > id two02</ div>
</ div> < div id = " three" class = " mini" > div id为three< div id = " three01" > id three01</ div>
</ div> </ body>
</ html>
4.基础过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 基础过滤选择器-应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 80px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "div:eq(0)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b2" ) . click ( function ( ) { $ ( "div:last" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b3" ) . click ( function ( ) { $ ( "div:not(.one)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b4" ) . click ( function ( ) { $ ( "div:even" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b5" ) . click ( function ( ) { $ ( "div:odd" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b6" ) . click ( function ( ) { $ ( "div:gt(3)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b7" ) . click ( function ( ) { $ ( "div:eq(3)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b8" ) . click ( function ( ) { $ ( "div:lt(3)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b9" ) . click ( function ( ) { $ ( ":header" ) . css ( "background" , "#0000FF" ) } ) } ) ; </ script>
</ head>
< body>
< h1> H1标题</ h1>
< h2> H2标题</ h2>
< h3> H3标题</ h3> < input type = " button" value = " 改变第一个 div 元素的背景色为 #0000FF" id = " b1" />
< input type = " button" value = " 改变最后一个 div 元素的背景色为 #0000FF" id = " b2" />
< input type = " button" value = " 改变class不为 one 的所有 div 元素的背景色为 #0000FF" id = " b3" />
< input type = " button" value = " 改变索引值为偶数的 div 元素的背景色为 #0000FF" id = " b4" />
< input type = " button" value = " 改变索引值为奇数的 div 元素的背景色为 #0000FF" id = " b5" />
< input type = " button" value = " 改变索引值为大于 3 的 div 元素的背景色为 #0000FF" id = " b6" />
< input type = " button" value = " 改变索引值为等于 3 的 div 元素的背景色为 #0000FF" id = " b7" />
< input type = " button" value = " 改变索引值为小于 3 的 div 元素的背景色为 #0000FF" id = " b8" />
< input type = " button" value = " 改变所有的标题元素的背景色为 #0000FF" id = " b9" />
< hr/>
< div id = " one" class = " mini" > div id为one
</ div>
< div id = " two" > div id为two< div id = " two01" > id two01</ div> < div id = " two02" > id two02</ div>
</ div> < div id = " three" class = " one" > div id为three class one< div id = " three01" > id three01</ div>
</ div>
</ body>
</ html>
5.内容过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 内容过滤选择器应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 80px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "div:contains('di')" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b2" ) . click ( function ( ) { $ ( "div:empty" ) . css ( "background" , "pink" ) } ) $ ( "#b3" ) . click ( function ( ) { $ ( "div.mini" ) . css ( "background" , "green" ) } ) $ ( "#b4" ) . click ( function ( ) { $ ( "div:parent" ) . css ( "background" , "yellow" ) } ) $ ( "#b5" ) . click ( function ( ) { $ ( "div:gt(3)" ) . css ( "background" , "#0000FF" ) } ) $ ( "#b6" ) . click ( function ( ) { $ ( "div:not(:contains('di'))" ) . css ( "background" , "pink" ) } ) } ) ; </ script>
</ head>
< body> < input type = " button" value = " 改变含有文本 ‘di’ 的 div 元素的背景色为 black" id = " b1" />
< input type = " button" value = " 改变不包含子元素(或者文本元素) 的 div 的背景色为 pink" id = " b2" />
< input type = " button" value = " 改变含有 class 为 mini 元素的 div 元素的背景色为 green" id = " b3" />
< input type = " button" value = " 改变含有子元素(或者文本元素)的div元素的背景色为 yellow" id = " b4" />
< input type = " button" value = " 改变索引值为大于 3 的 div 元素的背景色为 #0000FF" id = " b5" />
< input type = " button" value = " 改变不含有文本 di; 的 div 元素的背景色 pink" id = " b6" />
< hr/>
< div id = " xxxx" > < div id = " one" class = " mini" > div id为one</ div>
</ div> < div id = " two" > div id为two< div id = " two01" > id two01</ div> < div id = " two02" > id two02</ div>
</ div> < div id = " three" class = " one" > div id为three class one< div id = " three01" > id three01</ div>
</ div> < div id = " four" class = " one" > XXXXXXXXXX
</ div>
< div id = " five" class = " one" > </ div>
< div id = " mover" > 执行动画
</ div>
</ body>
</ html>
6.可见度过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 可见度过滤选择器-应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 30px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } div.visible { display : none; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "div:visible" ) . css ( "background" , "red" ) ; } ) $ ( "#b2" ) . click ( function ( ) { $ ( "div:hidden" ) . css ( "background" , "green" ) ; $ ( "div:hidden" ) . show ( ) ; } ) $ ( "#b3" ) . click ( function ( ) { var $inputs = $ ( "input:hidden" ) ; $inputs. each ( function ( ) { console. log ( "值是(dom方式)=" + this . value) ; console. log ( "值是(jquery方式)=" + $ ( this ) . val ( ) ) } ) } ) } ) ; </ script>
</ head>
< body> < input type = " button" value = " 改变所有可见的div元素的背景色为 #0000FF" id = " b1" /> < br/> < br/>
< input type = " button" value = " 选取所有不可见的div, 利用 jQuery 中的 show() 方法将它们显示出来, 并设置其背景色为 #0000FF" id = " b2" /> < br/> < br/>
< input type = " button" value = " 选取所有的文本隐藏域, 并打印它们的值" id = " b3" /> < br/> < br/>
< hr/> < input type = " hidden" value = " hidden1" />
< input type = " hidden" value = " hidden2" />
< input type = " hidden" value = " hidden3" />
< input type = " hidden" value = " hidden4" /> < div id = " one" class = " visible" > div id为one
</ div> < div id = " two" class = " visible" > div id为two
</ div> < div id = " three" class = " one" > div id为three
</ div> </ body>
</ html>
7.属性过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 属性过滤选择器-应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.mini { width : 30px; height : 30px; background : #CC66FF; border : #000 1px solid; font-size : 12px; font-family : Roman; } div.visible { display : none; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "div[title]" ) . css ( "background" , "green" ) ; } ) $ ( "#b2" ) . click ( function ( ) { $ ( "div[title = 'test']" ) . css ( "background" , "blue" ) ; } ) $ ( "#b3" ) . click ( function ( ) { $ ( "div[title != 'test']" ) . css ( "background" , "red" ) ; } ) $ ( "#b4" ) . click ( function ( ) { $ ( "div[title ^= 'te']" ) . css ( "background" , "yellow" ) ; } ) $ ( "#b5" ) . click ( function ( ) { $ ( "div[title $= 'est']" ) . css ( "background" , "white" ) ; } ) $ ( "#b6" ) . click ( function ( ) { $ ( "div[title *= 'es']" ) . css ( "background" , "black" ) ; } ) $ ( "#b7" ) . click ( function ( ) { $ ( "div[id][title *= 'es']" ) . css ( "background" , "pink" ) ; } ) } ) ; </ script>
</ head>
< body> < input type = " button" value = " 含有属性title 的div元素." id = " b1" /> < br/> < br/>
< input type = " button" value = " 属性title值等于test的div元素" id = " b2" /> < br/> < br/>
< input type = " button" value = " 属性title值不等于test的div元素(没有属性title的也将被选中)" id = " b3" /> < br/> < br/>
< input type = " button" value = " 属性title值 以te开始 的div元素" id = " b4" /> < br/> < br/>
< input type = " button" value = " 属性title值 以est结束 的div元素" id = " b5" /> < br/> < br/>
< input type = " button" value = " 属性title值 含有es的div元素" id = " b6" /> < br/> < br/>
< input type = " button" value = " 选取有属性id的div元素,然后在结果中选取属性title值含有“es”的 div 元素" id = " b7" /> < br/> < br/> < div id = " one" title = " test" > div id为one test
</ div> < div id = " one-1" title = " texxx" > div id为one-1 texxx
</ div> < div id = " one-2" title = " xxxest" > div id为one-2 xxxest
</ div> < div id = " one-3" title = " xxxesxxxxxt" > div id为one-3 xxxesxxxxxt
</ div> < div id = " two" title = " ate" > div id为two
</ div> < div id = " three" class = " one" > div id为three
</ div>
</ body>
</ html>
8.子元素过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 子元素过滤选择器示例-应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 70px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } div.visible { display : none; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( ".one:nth-child(2)" ) . css ( "background" , "yellow" ) ; } ) $ ( "#b2" ) . click ( function ( ) { $ ( ".one:nth-child(1)" ) . css ( "background" , "green" ) ; } ) $ ( "#b3" ) . click ( function ( ) { $ ( "div:last-child" ) . css ( "background" , "red" ) ; } ) $ ( "#b4" ) . click ( function ( ) { $ ( "div .one:only-child" ) . css ( "background" , "pink" ) ; } ) } ) ; </ script>
</ head>
< body> < input type = " button" value = " 每个class为one的div父元素下的第2个子元素" id = " b1" /> < br/> < br/>
< input type = " button" value = " 每个class为one的div父元素下的第一个子元素" id = " b2" /> < br/> < br/>
< input type = " button" value = " 每个class为one的div父元素下的最后一个子元素" id = " b3" /> < br/> < br/>
< input type = " button" value = " 如果class为one的div父元素下的仅仅只有一个子元素,那么选中这个子元素" id = " b4" /> < br/> < br/> < div class = " one" > < div id = " one" class = " one" > XXXXXXXXX id=one</ div> < div id = " two" class = " one" > XXXXXXXXX id=two</ div> < div id = " three" class = " one" > XXXXXXXXX id=three</ div> < div id = " four" class = " one" > XXXXXXXXX id=four</ div>
</ div> < div class = " one" > < div id = " five" class = " one" > XXXXXXXXX id=five</ div>
</ div>
</ body>
</ html>
3.细节说明
中间带空格的定位方式就是父元素 子元素的形式 先找出满足条件的父元素,然后找出父元素下满足条件的子元素
9.表单属性过滤选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 表单对象属性过滤选择器-应用实例</ title> < style type = " text/css" > div, span { width : 140px; height : 140px; margin : 20px; background : #9999CC; border : #000 1px solid; float : left; font-size : 17px; font-family : Roman; } </ style> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { $ ( "#b1" ) . click ( function ( ) { $ ( "input[type='text']:enabled" ) . val ( "台球" ) } ) $ ( "#b2" ) . click ( function ( ) { $ ( "input[type='text']:disabled" ) . val ( "足球" ) } ) $ ( "#b3" ) . click ( function ( ) { var $input = $ ( "input[type='checkbox']:checked" ) ; alert ( $input. length) } ) $ ( "#b4" ) . click ( function ( ) { var $select = $ ( "select option:checked" ) ; $select. each ( function ( ) { alert ( $ ( this ) . val ( ) ) } ) } ) } ) ; </ script>
</ head>
< body>
< input type = " button" value = " 利用 jQuery 对象的 val() 方法改变表单内 type=text 可用 <input> 元素的值" id = " b1" /> < br/> < br/>
< input type = " button" value = " 利用 jQuery 对象的 val() 方法改变表单内 type=text 不可用 <input> 元素的值" id = " b2" /> < br/> < br/>
< input type = " button" value = " 利用 jQuery 对象的 length 属性获取多选框选中的个数" id = " b3" /> < br/> < br/>
< input type = " button" value = " 利用 jQuery 对象的 text() 方法获取下拉框选中的内容" id = " b4" /> < br/> < br/>
< br>
< input type = " text" value = " 篮球1" />
< input type = " text" value = " 篮球2" />
< input type = " text" value = " 篮球3" disabled = " true" />
< input type = " text" value = " 篮球4" disabled = " true" />
< br>
< h1> 选择你的爱好</ h1>
< input type = " checkbox" value = " 爱好1" /> 爱好1
< input type = " checkbox" value = " 爱好2" /> 爱好2
< input type = " checkbox" value = " 爱好3" /> 爱好3
< input type = " checkbox" value = " 爱好4" /> 爱好4
< br>
< h1> 选项如下:</ h1>
< select name = " job" size = 9 multiple = " multiple" > < option value = " 选项1" > 选项1^^</ option> < option value = " 选项2" > 选项2^^</ option> < option value = " 选项3" > 选项3^^</ option> < option value = " 选项4" > 选项4^^</ option> < option value = " 选项5" > 选项5^^</ option> < option value = " 选项6" > 选项6^^</ option>
</ select> < select id = " hsp" name = " edu" > < option value = " 博士" > 博士^^</ option> < option value = " 硕士" > 硕士^^</ option> < option value = " 本科" > 本科^^</ option> < option value = " 小学" > 小学^^</ option>
</ select>
</ body>
</ html>
10.表单选择器
1.基本介绍
2.代码实例
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> 表单选择器-应用实例</ title> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script type = " text/javascript" > $ ( function ( ) { var $button = $ ( ":button" ) ; alert ( "$button 大小=" + $button. length) var $button2 = $ ( "input[type='button']" ) ; alert ( "$button2 大小=" + $button2. length) var $button3 = $ ( "button" ) ; alert ( "$button3 大小=" + $button3. length) } ) ; </ script>
</ head>
< body>
< form> < input type = " text" /> < br/> < input type = " checkbox" /> < br/> < input type = " radio" /> < br/> < input type = " image" src = " ../image/2.png" height = " 100" /> < br/> < input type = " file" /> < br/> < input type = " submit" /> < br/> < input type = " reset" /> < br/> < input type = " password" /> < br/> < input type = " button" value = " 按钮1" /> < br/> < select> < option/> </ select> < br/> < textarea> </ textarea> < br/> < button> 按钮2</ button> < button> 按钮3</ button> < br/>
</ form>
</ body>
</ html>
11.常用选择器
一般使用父子/基本选择器 —> 属性选择器 —> 过滤即可解决大多数情况
1.基本选择器
2.可见度过滤选择器
3.子元素过滤选择器
4.属性过滤选择器
5.表单属性过滤选择器
6.表单选择器
6.作业
1.练习一
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> Title</ title> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script> $ ( function ( ) { var $p = $ ( "p" ) ; $p. each ( function ( ) { var text = $ ( this ) . text ( ) ; $ ( this ) . click ( function ( ) { alert ( text) } ) } ) } ) </ script>
</ head>
< body>
< p> 段落一</ p>
< p> 段落二</ p>
< p> 段落三</ p>
</ body>
</ html>
2.练习二
<! DOCTYPE html >
< html lang = " en" >
< head> < meta charset = " UTF-8" > < title> Title</ title> < script type = " text/javascript" src = " ./jquery/jquery-3.6.0.min.js" > </ script> < script> $ ( function ( ) { $ ( "table tr:even" ) . css ( "background" , "red" ) $ ( "table tr:odd" ) . css ( "background" , "blue" ) } ) </ script>
</ head>
< body>
< table border = " 1" width = " 500" > < tr> < td> 1</ td> < td> 1</ td> </ tr> < tr> < td> 2</ td> < td> 2</ td> </ tr> < tr> < td> 3</ td> < td> 3</ td> </ tr> < tr> < td> 4</ td> < td> 4</ td> </ tr> < tr> < td> 5</ td> < td> 5</ td> </ tr> < tr> < td> 6</ td> < td> 6</ td> </ tr>
</ table>
</ body>
</ html>