var content=document.getElementById( content
var items=content.getElementsByTagName( ul
var itemss=items[2].getElementsByTagName( li //獲取第二個li標簽
或
var p=document.getElementById( a
var ul=p.childNodes.item(0);
var lis=ul.childNodes;
for(var i=0;i lis.length;i++){
alert( Item +i+ : +lis.item(i).innerHTML);
}
$(function(){
$( ul ).each(function(){
var y = $(this).children().last();
alert(y.text());
});
});
li > li a href= 回答榜 /a /li
li a href= 提問榜 /a /li
li a href= 滿意榜 /a /li
/ul
點擊那個就把在那個 li 的追加 >
比如:點擊了回答榜 變成
ul > li a href= 積分榜 /a /li
li > li a href= 提問榜 /a /li
li a href= 滿意榜 /a /li
/ul
$(function(){
$( .anserdh li a ).click(function(){
$( .anserdh li ).removeClass( qhbg
$(this).parent().addClass( qhbg
})
})
jquery如何定位倒數第二個元素,如一個p里有5個ul,那jquery如何才能鎖定到倒數第一個ul,第二個ul,第一個ul樣式$( p ul ).eq(-1)
$( p ul ).eq(-2)
$( ul li:first-child ).css( backgroundColor , #000 );
jquery中.each()遍歷元素的一些學習
!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 tab選項卡 /title style type= text/css ul,li{list-style: none;margin: 0px; padding: 0px;} li{float: left;width: 80px; height: 30px; background-color: #ccc; border: 2px solid #fff;text-align:center; line-height:30px;} #content{clear:left; width:336px; height: 180px; background-color: #999; color:white;} #content p{display: none} #content .consh{display: block;} #title .titsh{background-color: #999;border:2px solid #999; color:#fff} /style script type= text/javascript src= jquery.js /script script type= text/javascript $(function(){ $( li ).each(function(index){ $(this).mouseover(function(){ $( #title .titsh ).removeClass( titsh $( #content .consh ).removeClass( consh $(this).addClass( titsh $( #content p:eq( +index+ ) ).addClass( consh /script /head body p id= tab p id= title li >
測試的結果是正常,后來在一個實際使用的頁面中使用的時候,發現上面的li列表變動的時候,下面的p區塊不跟著變動不同的區塊,以為是css樣式和實際使用的頁面中其他的樣式沖突了,將css選擇器全部改成獨有的之后,發現還是這個問題,于是判斷應該是這里:
$( #title .titsh ).removeClass( titsh $( #content .consh ).removeClass( consh $(this).addClass( titsh $( #content p:eq( +index+ ) ).addClass( consh 第一句,第二句取出樣式的時候,沒有問題,第三局給當前的li標簽加上titsh的css樣式也正常,就是最后一句 給通過p:eq(index)獲取到的p區塊加樣式的時候失敗。于是我在$( li ).each(function(index){$(this).mouseover(function(){這兩句之間加了一個alert(index)彈窗,看看效果,發現有10幾個li標簽的索引值被alert出來,一想原來實際這個頁面中還有其他的li標簽,所以導致each()迭代出來的索引值和下面p區塊的索引值對應不上,這樣上面li標簽變動的時候,下面的p區塊就不跟著變了,于是我將js代碼改了一下:
script type= text/javascript $(function(){ $( #title ul li ).each(function(index){ $(this).click(function(){ $( #title .titsh ).removeClass( titsh $( #content .consh ).removeClass( consh $(this).addClass( titsh $( #content p:eq( +index+ ) ).addClass( consh /script
給要用.each()迭代的li元素的選擇器加了限制,讓他只能找我選項卡中的li標簽來each出索引值,問題解決,可以睡覺了!
以上就是Jquery和JS獲取ul中li標簽的詳細內容,html教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答