开发者

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 .params inside the nested <ul>.

You can write $('#li1>.widget .param') to get all .params in .widgets directly inside #li1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜