abstract:<!DOCTYPE html> <html> <head> <title>jquery选择器</title> <script type="text/javascript" src="jquery-3.4.0.min.js"></script>
<!DOCTYPE html> <html> <head> <title>jquery选择器</title> <script type="text/javascript" src="jquery-3.4.0.min.js"></script> <!-- css设置元素的属性 --> <style type="text/css"> .one,.two{height: 100px;width: 100px;background:red;} span{color: #fff;} </style> <!-- 通过jquery去改元素属性 --> <script type="text/javascript"> $(document).ready(function(){ // <!-- 基本选择器 --> $(".one").css({"background":'skyblue','height':'200px','width':'200px'}) $("span").css("color","black") $('.two,#src,b').css({'background':'pink','color':'#fff'}) // 层级选择器 $('ul>li').css({'background':'blue','color':"#fff"}) $('ul li').css('background','red') $('input+button').css({'width':'100px','height':'40px', 'background':'pink','border':'0','marginLeft':'30px'}) $('label~input').css({'background':'blue','width':'300px', 'height':'30px','marginLeft':'10px'}) // 顺序选择器 $('p:first').css('background','red') $('p:last').css('background','green') $('p:gt(0)').css('background','skyblue') }) </script> </head> <body> <!--《 基本选择器 --> <div class="one"><span>jquery的选择器</span></div> <div class="two"><span id="src">匹配多个选择器</span><b style="font-size: 2em">123</b></div> <!-- 层级选择器 --> <ul> <li>晴天</li> <li> <div> <ul> <li>阴天</li> </ul> </div> </li> <li>下雨</li> <li>台风</li> </ul> <label>天气:</label> <input type="text" name=""> <button>submit</button><br> <label>预报:</label> <input type="password" name=""> <!-- 顺序选择器 --> <p>这个是第一段</p> <p>这个是第二段</p> <p>这个是第三段</p> <p>这个是第四段</p> <p>这个是第五段</p> </body> </html>
jQuery选择器
一、基本选择器
$('#id名')根据给定的id来匹配到元素
例如:$('#box').css('background','red')
$('.class名')根据给定的class来匹配到元素
例如:$('.box').css('background','blue')
$('element')根据给定的标签名来匹配到元素
例如:$('span').css('font-size','30px')
$('*')匹配所有元素
例如: $('*').css('font-family','宋体')
$('#id,.class名,element')匹配到页面中多个选择器
例如: $('#box,.box,span').css('color','pink')
二、层级选择器
$('ul>li').css('list-style','none') //匹配 ul下的所有li子层级,即父层级下所有的子层级
$('ul li').css('list-style','none')//匹配ul下所有的有li的层级,即祖先层级下所有的后代
$('input+button').css('height','50px')//匹配input同层级紧跟后面button元素
$('label~input').css('background','pink')//匹配label后面所有的input
三、顺序选择器
$('p:first').css('color','red') //匹配第一个P标签元素
$('p:last').css('color','blue') //匹配最后一个 P标签元素
$('p:gt(1)').css('font-size','30px') //匹配从第三个开始的所有P标签元素
即($(':gt(x)')表示大于值x的元素)
$('p:lt(2)').css('color','red') //匹配第一个和第二个P标签元素
即($(':lt(x)')表示小于值x的元素)
$('p:eq(1)').css('color','red') //匹配第二个P标签元素
即($(':eq(x)')表示等于值x的元素)
$('p:odd').css('background','#ccc') //匹配奇数的P标签元素
即($(':odd')奇数顺序)
$('p:even').css('background','pink') //匹配偶数的P标签元素
即($(':even')偶数顺序)
$('p:not(#box)').css('background','red') //匹配不是#box的所有元素
即($(':not(selector)')匹配不是selector的所有元素)
四、内容选择器
$('div:contains(jun)').css('background','blue') //在div选择文本维jun的文本
即($(':contains(text)') 匹配包含给定文本(text)的元素)
<div>jun</div>
$('div:has(span)').css('color','red') //匹配div里面有span的元素
即($(':has(selector)')匹配包含特定选择器元素的元素)
<div><span>PHP中文网</span></div>
$('div:empty').css('background','red') //匹配div不含有内容元素
即($(':empty')匹配不含有内容的元素(即 不包含子元素或者文本的空元素))
<div></div>
<div>1</div>
<div><b></b></div>
$('div:parent').css('background','red') //与empty相反
即($(':parent')匹配含有子元素或者文本的元素)
五、属性选择器
一、$('[属性名]')匹配包含给定属性的元素
栗子:$('input[type]').css('background','pink')
<input type="text"name="">
二、 $('[attribute=value]')匹配给定属性是某个特定值的元素
栗子: $('input[type=button]').css('background','blue')
<input type="button" value="按钮">
三、$('[attribute!=value]')匹配所有不含有指定值的属性,或者说是属性不等于特定值的元素
栗子:$('input[type!=text]').css('background','red')
<input type="text" name="new" id="woman"><br>
<input type="password" name="new1" id="man"><br>
<input name="new"id="new"><br>
四、$('[attribute ^= value]')匹配给定属性是以某些值开始的元素
$('[attribute $= value]')匹配给定属性是以某些值结尾的元素
$('[attribute *= value]')匹配给定属性包含某些值的元素
栗子:$('input[type ^=t ]').css('background','red')
$('input[type $=n ]').css('background','red')
$('input[type *=o ]').css('background','blue')
<input type="text" name="new" id="woman"><br>
<input type="password" name="new1" id="man"><br>
<input type="button" value="按钮">
五、$('attrSel[1] attrSel[1] attrSel[1]')复合选择器,需要同时满足多个条件时使用
栗子:$('input[id][name*=n]').css('background','red')
<input type="text" name="new" id="woman"><br>
<input type="password" name="new1" id="man"><br>
<input type="button" value="按钮">
六、表单选择器
一、$(':enabled')所有激活的input元素(可以使用的input元素)
栗子:$(document).ready(function(){
$(':enabled').css('background','pink')
})
二、$(':disabled')所有禁用的input元素(不可以使用的input元素)
栗子:$(document).ready(function(){
$(':disabled').css('background','red')
})
<form>
输入框1<input type="text" name=""><br>
输入框2<input type="text" name=""><br>
输入框3<input type="text" name="" disabled><br>
输入框4<input type="text" name=""><br>
</form>
三、$(':selected')所有被选取的元素,针对于select元素
栗子 : $(':selected').css('color','blue')
<select>
<option>摩羯座</option>
<option selected>双鱼座</option>
<option>射手座</option>
<option>天蝎座</option>
</select>
四、$(':checked')所有被选中的input元素
栗子:$(document).ready(function(){
$(':checked').parent().css('color','red')
})
<form>
爱好:
<label><input type="checkbox" name="">看书</label>
<label><input type="checkbox" name="" checked>游泳</label>
<label><input type="checkbox" name="">游戏</label>
</form>
Correcting teacher:查无此人Correction time:2019-04-25 13:37:06
Teacher's summary:完成的不错。jq比js简单很多,多练习可以代替常规js。继续加油。