您好,欢迎访问一九零五行业门户网

Jquery和JS获取ul中li标签

js 获取元素下面所有的li 
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;i102b2aba98ff17351034c9eea133087c 点击的是那个25edfb22a4f469ecb59f1190150159c6
3d4bc9d7f79b2a13ff6f28661e2d6d07
29648217c88beffc99ae7948b95b337528b32422842eba366a6e07bd1bffce8b积分榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c68fb0647cc270c6c60ce1679670afa6fc回答榜 5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6e5043b093cc4bc2a46bfcf330245fc36提问榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6e5043b093cc4bc2a46bfcf330245fc36满意榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
点击那个就把在那个25edfb22a4f469ecb59f1190150159c6的追加class=qhbg样式
比如:点击了回答榜 变成
 3d4bc9d7f79b2a13ff6f28661e2d6d07
      861f448a2c47375a88adcb954dabc95328b32422842eba366a6e07bd1bffce8b积分榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      29648217c88beffc99ae7948b95b33758fb0647cc270c6c60ce1679670afa6fc回答榜 5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6e5043b093cc4bc2a46bfcf330245fc36提问榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
      25edfb22a4f469ecb59f1190150159c6e5043b093cc4bc2a46bfcf330245fc36满意榜5db79b134e9f6b82c0b36e0489ee08edbed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
$(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"> <ul> <li class="titsh">选项一</li> <li>选项二</li> <li>选项三</li> <li>选项四</li> </ul> </p> <p id="content"> <p class="consh">内容一</p> <p>内容二</p> <p>内容三</p> <p>内容四</p> </p> </p> </body> </html>
测试的结果是正常,后来在一个实际使用的页面中使用的时候,发现上面的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标签的详细内容。
其它类似信息

推荐信息