如何访问ul标签中的li元素 [英] How to access li elements within ul tag
问题描述
我想知道如何访问ul内的每个li?我不知道ul的名字是什么。
I want to know how to access each li within the ul? I am not sure exactly what the name of the ul is.
以下是代码:
<ul class= "selectBox-options exp-pdp-size-dropdown exp-pdp-dropdown" style>
<li class = " omitted">
more li here omitted
</ul>
我的主要目标是让用户在下拉菜单中点击一个尺寸,自动检测ul中的特定li(8)
My main goal is to have a user click a size in a dropdown lets say size 8 and it will automatically detect that specific li(8) inside the ul
推荐答案
您可以使用 querySelectorAll
:
var array_of_li = document.querySelectorAll("ul.selectBox-options li");
如果您不确定 ul
将有 selectBox-options
类,然后删除 .selectBox-options
部分 - 但记住它会得到你每个 li
。
If you're not sure that the ul
will have the selectBox-options
class, then delete the .selectBox-options
part - but remember that it will get you every li
on the page then.
如果你使用jQuery, ,并且基本上做同样的事情:
If you're using jQuery, then this is a bit more compatible, and does basically the same thing:
var li = $("ul.selectBox-options li");
这篇关于如何访问ul标签中的li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!