我有:
<li id="1">
This is what I want to get
<li id="2">
not needed to be selected
<li id = "3">
not needed...
</li>
</li>
</li>
我想用 jQuery 获取 id = 1 文本的 li,这就是我所拥有的:
var id = "1"
var needed = $('li[id='+cat_id+']').text();
但这得到了所有的文本,我怎么只能得到This is what I want to get
.
更新
你是对的,那是错误的,所以现在是:
<li id="1">
This is needed
<ul>
<li id = "2">Not needed</li>
<li id = "3"> Not needed too </li>
</ul>
</li>
This is needed
在上面的示例中,考虑到它的 id =“1” ,我该如何选择?