Jquery get elements
HI I have following problem, i have a ul list like this one
<ul class="ul">
<li id="li1">
<span class="widget">
<input name='try1' value="try1" class="param" />
</span>
<ul>
开发者_Go百科 <li class="li2">
<span class="widget">
<input name='try1' value="try1" class="param" />
<input name='try2' value="try2" class="param" />
</span>
</li>
</ul>
</li>
<li id="2"></li>
</ul>
my problem is when i try to get inputs in this way
$('#li1 .param').each(function(){
alert($(this).attr('name'));
});
each returns to me try1 try1 try2 But i need only these ones between span if someone knows how to get them? Thank you in advance !
Hard to understand, but I assume you mean that you only want the ones that descend from the child <span>
element instead of all <span>
elements.
If so, do this:
$('#li1 > span .param').each(function(){
alert($(this).attr('name'));
});
This uses the the child-selector
(docs) to target .param
elements that descend from a <span>
element that is a child of #li1
.
It sounds like you don't want the .param
s inside the nested <ul>
.
You can write $('#li1>.widget .param')
to get all .param
s in .widget
s directly inside #li1
.
精彩评论