日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jquery选择器《-》

發布時間:2025/3/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jquery选择器《-》 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

1.DOM篩選,遍歷查找相關方法

2.選擇器中特殊符號的處理

3.改寫原生js例子


????? a.表格隔行變色

???? ?b.tab標簽頁


4.選擇器的優化準則(初級)



篩選

eq()選擇指定索引的元素

filter(表達式)篩選指定表達式的元素

first()選擇第一個元素

last()選擇最后一個元素

is()檢測是否元素返回布爾值

has()保留包含特定后代的元素,去掉那些不含有指定后代的元素

not()從匹配的元素集合中移除指定的元素。

map()將一組元素轉換成其他數組

slice()根據指定的下標范圍,選取匹配的元素集合


<script>
// $('p:eq(0)').css('background','red');
// $('p').filter('#second').css('background','red');
// $('p').first().css('background','red');
// $('p').last().css('background','red');

/* $('p').click(function(){
if($(this).is('.first')){
$(this).css('background','red');
}
});*/

// $('p').not('#second').css('background','red');
//?
// $('p').slice(-2,-1).css('background','red');

</script>


<body>
?<form action="">
??<input type="text" value="111" />
??<input type="text" value="222" />
??<input type="text" value="333" />
?</form>
?<p></p>

?<script>
??var arr = $('input').map(function(){
???return $(this).val()
??}).get().join(',');

??alert(typeof arr);
??$('p').html(arr);
?</script>
</body>

遍歷查找

a.children()選取子元素

b.parent()選取父元素

c.parents()選取祖先元素

d.parentsUntil()所有的父輩元素,直到遇到匹配的那個元素為止?1.6+? //與有參數的parents()等價

e.offsetParent()返回父元素中第一個其position設為relative或者absolute

的元素,僅對可見元素有效

f.next()選擇后面緊臨的兄弟元素

g.nextAll()查找當前元素之后所有的同輩元素

h.nextUntil()所有的同輩元素,直到遇到匹配的那個元素為止?1.6+

i.prev()前一個兄弟元素

j.prevAll()前面所有的兄弟元素

k.prevUntil()所有的兄弟元素,直到遇到匹配的那個元素為止?1.6+

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
?<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
?<title>demo3</title>
?<script src="jquery.js"></script>
</head>
<body>
?
?<div id="outer" style="position:relative">
??outer
??<div id="wrap" style="">
???wrap
???<div>111111111</div>
???<div>222222222</div>
???<p id="p1">我是<span>第1個P</span>標簽</p>
???<p id="p2">我是第2個P標簽</p>
???<p>我是第3個P標簽</p>
???<div>我是div標簽</div>
??</div>
?</div>
?<script>
?// $('#wrap').children('p').css('background','red');
?// $('#p1').parent().css('background','red');
?// $('#p1').parents('#wrap').css('background','red');
?//?
?//$('#p1').offsetParent().css('background','red');
?//$('#p1').next().css('background','red');
?//$('#p1').nextAll('p').css('background','red');
?//
?//$('#p2').prev().css('background','red');
?//$('#p2').prevAll('div').css('background','red');
?//
?//$('#p2').siblings('div').css('background','red');
?//?
?//$('span').parent().css('background','red').end().css('background','#abcdef');
?//$('span').css('background','#abcdef');
?//
?$('#p1').nextAll().addBack().css('background','red');
?</script>

</body>
</html>



l.sinlings()前后所有的兄弟元素

m.closest()從元素本身開始,在DOM?樹上逐級向上級元素匹配,并返回最先匹配的祖先元素

n.contents()元素的子元素,包括文字和注釋節點

o.end()終止在當前鏈的最新過濾操作,并返回匹配的元素的以前狀態

p.andself()1.8版本中已廢棄

q.addBack()添加堆棧中元素集合到當前集合,一個選擇性的過濾選擇器

r.each()遍歷一個jQuery對象,為每個匹配元素執行一個函數

<script src="jquery.js"></script>
</head>
<body>
?<form action="">
??<input type="text" name="" id="" value="第1個input的值" />
??<input type="text" name="" id="" value="第2個input的值" />
??<input type="text" name="" id="" value="第3個input的值" />
??<input type="text" name="" id="" value="第4個input的值" />
?</form>
?<script>
??/*$('input').each(function(){
???alert($(this).val());
??});*/
?</script>


特殊符號的處理

???? 使用轉義符

<body>
?<form action="">愛好:
??<input type="checkbox" name="gender[]" id="" value="看書" />
??<input type="checkbox" name="gender[]" id="" value="籃球" />
??<input type="checkbox" name="gender[]" id="" value="編程" />
??<input type="submit" value="提交" /><input type="reset" value="重寫" />
?</form>

?<script>

?/*?$('input[name=gender\\[\\]]').click(function(){
???alert($(this).val());
??});*/

?$('input[type=\'checkbox\']').click(function(){
???alert($(this).val());
??});
?</script>
</body>

改寫實例


a.表格隔行變色

//js

<script>
??var oTable = document.getElementById('table');
??var aTr = oTable.getElementsByTagName('tr');
??//alert(aTr.length);
??
??for(var i=0;i<aTr.length;i++){
???
???if(i%2==1){
????aTr[i].style.background="yellow";
???}else{
????aTr[i].style.background="#abcdef";
???}
??}

?</script>

//jquery

<script>
??/*$('#table tr:even').css('background','#abcdef');
??$('#table tr:odd').css('background','yellow');*/

??$('#table tr').filter(':even').css('background','#abcdef').end().filter(':odd').css('background','yellow');
?</script>

b.tab標簽頁

//js

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
?<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
?<title>原生js的tab標簽頁</title>
?<style>
??*{
???padding: 0;
???margin: 0;
??}
??ul{
???list-style-type: none;
??}
??body{
???margin: 50px ;
??}
??#ul{
???height: 30px;
???margin-bottom: 10px;
??}
??#ul li{
???height: 30px;
???line-height: 30px;
???padding: 0 15px;
???border: 1px solid #abcdef;
???float: left;
???margin-right: 3px;
???cursor: pointer;
??}
??#ul li.current{
???background: #abcdef;
??}

??#content div{
???width: 300px;
???height: 200px;
???border: 1px solid #abcdef;
???display: none;
??}
??#content div.show{
???display: block;
??}
?</style>
</head>
<body>
?<ul id="ul">
??<li>php</li>
??<li>ruby</li>
??<li>python</li>
?</ul>
?<div id="content">
??<div>php。。。。。。介紹</div>
??<div>ruby。。。。。。介紹</div>
??<div>python。。。。。。介紹</div>
?</div>

?<script>
??var ul = document.getElementById('ul');
??var li = ul.getElementsByTagName('li');
??var content = document.getElementById('content');
??var div = content.getElementsByTagName('div');

??for (var i = 0; i < li.length; i++) {
???li[i].index = i;
???li[i].οnclick=function(){
????for (var i = 0; i < li.length; i++) {
?????li[i].className='';
?????div[i].style.display='none';
????};

????this.className='current';
????div[this.index].style.display='block';
???}
??};

?</script>

</body>
</html>


//jquery

<script>
??$('#ul li').click(function(){

???//1、點擊li的時候要切換樣式
???//$(this).addClass('current').siblings().removeClass('current');
???//2、根據li的索引值,來確定哪個div顯示,其它div隱藏
???//$('#content>div').eq($(this).index()).show().siblings().hide();


???//$(this).addClass('current').siblings().removeClass('current').parent().next().children().eq($(this).index()).show().siblings().hide();

??});

???$(this).addClass('current').siblings().removeClass('current').parent().next().find('div').eq($(this).index()).show().siblings().hide();

??});
?</script>


jquery選擇器的優化


1.優先使用id選擇器

2.class選擇器前添加標簽名

3.采用find(),而不使用上下文查找

4.強大的鏈式操作比緩存更快

5.從左至右的模型1.3+版本更新

轉載于:https://my.oschina.net/u/1455528/blog/219791

總結

以上是生活随笔為你收集整理的jquery选择器《-》的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。