abstract:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>選擇器大全</title> <script type="text/javascript" src="static
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>選擇器大全</title> <script type="text/javascript" src="static/jQuery/jquery-3.4.1.js"></script> <style type="text/css"> div{width:200px;height:200px;background:#666;margin-top:10px;} </style> </head> <body> <script type="text/javascript"> $(document).ready(function(){ // 1.$('#id名') $('.class名') $('element') $('*') $('#id,.claclass名') $('#box').css('background','red'); $('.box').css('background','blue'); $('span').css('font-size','30px'); $('*').css('font-family','宋體'); $('#box,.box,span').css('color','pink'); //2.$('#id名') $('.class名') $('element') $('*') $('#id,.claclass名') // 給定的父級(jí)元素下匹配所有的子元素:$('父級(jí)元素 > 子級(jí)元素') // 給定的祖先元素下匹配所有的后代元素:$('祖先元素 后代元素') // 匹配緊跟在prev元素后面的next元素:$('prev + next')(同級(jí)的元素) // 匹配prev元素后面所有的siblings元素:$('prev ~ siblings') $('ul li').css('list-style','none'); $('input+button').css('height','50px'); // $('label~input').css('background','pink'); // 3.順序選擇器 // 1、順序 // $(':first') 第一個(gè)元素 // $(':last') 最后一個(gè)元素 // 2、比較(x的順序是從0開始) // $(':gt(x)')表示大于值x的元素 // $(':lt(x)')表示小于值x的元素 // $(':eq(x)')表示等于值x的元素 // 3、奇偶數(shù) // $(':odd')奇數(shù)順序 // $(':even')偶數(shù)順序 // 4、非 // $(':not(selector)')匹配不是selector的所有元素 // $('p:first').css('color','red'); // $('p:last').css('color','blue'); // $('p:gt(1)').css('font-size','30px'); // $('p:lt(3)').css('color','blue'); // $('p:eq(3)').css('color','blue'); // $('p:odd').css('color','red'); // $('p:even').css('color','red'); $('p:not(#box)').css('background','#ccc'); // 4.內(nèi)容選擇器 // 語法: // $(':contains(text)') 匹配包含給定文本(text)的元素 // $(':has(selector)')匹配包含特定選擇器元素的元素 // $(':empty')匹配不含有內(nèi)容的元素(即 不包含子元素或者文本的空元素) // $(':parent')匹配含有子元素或者文本的元素 $('div:contains(jun)').css('background','blue'); $('div:has(span)').css('color','red'); $('div:empty').css('background','green'); // $('div:parent').css('background','red'); // 5.屬性選擇器 // 語法: // $('[屬性名]')匹配包含給定屬性的元素 // $('[attribute=value]')匹配給定屬性是某個(gè)特定值的元素 //$('[attribute!=value]')匹配所有不含有指定值的屬性,或者說是屬性不等于特定值的元素 // $('[attribute ^= value]')匹配給定屬性是以某些值開始的元素 // $('[attribute $= value]')匹配給定屬性是以某些值結(jié)尾的元素 // $('[attribute *= value]')匹配給定屬性包含某些值的元素 //$('attrSel[1] attrSel[1] attrSel[1]')復(fù)合選擇器,需要同時(shí)滿足多個(gè)條件時(shí)使用 // $('input[type]').css('background','pink'); // $('input[type=button]').css('background','blue'); // $('input[type!=text]').css('background','blue'); // $('input[type^=p]').css('background','red'); // $('input[type$=n]').css('background','green'); // $('input[type*=t]').css('background','coral'); $('input[id][name*=liu]').css('background','red'); // 6.表單選擇器 // 語法: // $(':enabled')所有激活的input元素(可以使用的input元素) // $(':disabled')所有禁用的input元素(不可以使用的input元素) // $(':selected')所有被選取的元素,針對于select元素 // $(':checked')所有被選中的input元素 $(':enabled').css('background','pink'); $(':disabled').css('background','red'); $(':selected').css('color','blue'); $(':checked').parent().css('color','red');//parent()會(huì)印象下拉列表選項(xiàng) }) </script> <!-- 1.基本選擇器 --> <div id="box">大家好</div> <div>我是peter</div> <span>PHP中文網(wǎng)</span> <br> <hr> <!-- 2.層級(jí)選擇器 --> <ul> <li>1</li> <li>2</li> <div> <li>2_1 <!-- <div> <li>2_2</li> </div> --> </li> </div> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> <form action=""> <label for="">姓名:</label> <input type="text" name=""> <button>提交</button> <input type="text" name=""> </form> <br> <hr> <!-- 3.順序選擇器 --> <p id="box">1</p> <p>2</p> <p>3</p> <p>4</p> <p>5</p> <p>6</p> <br> <hr> <!-- 4.內(nèi)容選擇器 --> <div>jack</div> <div>jun</div> <div>jack cheng</div> <div>jion</div> <div><span>php中文網(wǎng)</span></div> <div></div> <div><b></b></div> <br> <hr> <!-- 5.屬性選擇器 --> <form> <label for="">1</label><input type="text" name="new" id="woman"><br> <label for="">2</label><input type="password" name="new1" id="man"><br> <label for="">3</label><input name="liu" id="new"><br> <label for="">4</label><input type="button" name="" value="按鈕"><br> </form> <br> <hr> <!-- 6.表單選擇器 --> <form action=""> 輸入框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> <select> <option>摩羯座</option> <option selected>雙魚座</option> <option>射手座</option> <option>天蝎座</option> </select> <br> 愛好: <label><input type="checkbox" name="">看書</label> <label><input type="checkbox" name="" checked>游泳</label> <label><input type="checkbox" name="">游戲</label> </form> </body> </html>
Correcting teacher:天蓬老師Correction time:2019-07-30 09:32:53
Teacher's summary:jQ中的選擇器眾多, 但常用的并不多, 建議把學(xué)習(xí)重點(diǎn)放在一些最基本的選擇器上