Home>Article>Web Front-end> Jquery and JS get the li tag in ul
js Get all li under the element
var content=document.getElementById("content");
var items=content.getElementsByTagName("ul");
var itemss=items[2].getElementsByTagName("li");//Get the second li tag
or
var p=document.getElementById('a');
var ul=p.childNodes.item(0);
var lis=ul.childNodes;
for(var i=0;i
}
$(
function
(){
##$("ul"
).each (
function
(){
var
y = $(
this
).children().
last
();
alert (y.text());
});
});
##jquery Gets the
For example: click answer The list becomes
## " >Points list
$(this).parent().('qhbg');
})
$("p ul").eq(-1)
$("p ul").eq(-2)
$(
'ul li:first-child'
).css(
'backgroundColor'
,
'#000'
);
Some learning about .each() traversing elements in jquery
##
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">tab选项卡
内容一
内容二
内容三
内容四
The test results were normal. Later, when I used it on an actual page, I found that when the li list above was changed, the p block below did not change with different blocks, thinking it was a css style. It conflicts with other styles in the actual page. After changing allcss selectorsto unique ones, I found that the problem still exists, so I judged that it should be here:
$("#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代码改了一下:
The above is the detailed content of Jquery and JS get the li tag in ul. For more information, please follow other related articles on the PHP Chinese website!