开发者

jQuery what instead $(this).parent().children()

开发者 https://www.devze.com 2023-03-09 06:21 出处:网络
Just a quick example: <p> <span class=\"example\"></span> <input type=\"text\" n开发者_JS百科ame=\"e_name\" id=\"e_id />

Just a quick example:

<p>
    <span class="example"></span>
    <input type="text" n开发者_JS百科ame="e_name" id="e_id />
</p>
<script type="text/javascript">
    $('input').click(function(){
        $(this).parent().children('span').text('Suprise!');
    }
</script>

What can I use instead parent().children()?

I think it's a bit inelegant piece of code. Is any function i.e : $(this).fun('span').text('just better'); ??


$(this).siblings('span').text('Suprise!');


$(this).siblings('span').text('Surprise!');

Would be the equivalent without traversing up the DOM and then back down (I mean, it still does it, but at least you're not doing it manually).


Slightly more elegant is .prev()

$(this).prev('span').text('Surprise!');

you can read more about it here: Prev

edit: read the markup backwards, prev is better, not next.


try this:

$(this).prev('span').text('Surprise!');


$("span.example",$(this).parent()).text('Suprise!');
0

精彩评论

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

关注公众号