开发者

How can I select a clicked item AND a subsequent item - Jquery

开发者 https://www.devze.com 2023-03-01 12:51 出处:网络
how can I select the item just clicked AND a subsequent item, using Jquery... At the moment whenever I try to use the \'this\' attribute along side another selector, it stops working...

how can I select the item just clicked AND a subsequent item, using Jquery... At the moment whenever I try to use the 'this' attribute along side another selector, it stops working...

HTML

<a href="#" class="refLink">9</a>
<span class="refPanelFloating">Blah blah blah</span>

T开发者_JAVA技巧ried Jquery

$('a.refLink').click(function () {
    $(this,'span.refPanelFloating').wrapAll('<span class="refWrap" />');
});

Desired result

<span class="refWrap">    
    <a href="#" class="refLink">9</a>
    <span class="refPanelFloating">Blah blah blah</span>
</span>

At the moment I can only get it to work by selecting the initial 'a.refLink' tag... but on the final page there will be lots of them...

Thanks


Try this:

$('a.refLink').click(function () {
    $(this).next().andSelf().wrapAll('<span class="refWrap" />');
});

See http://jsfiddle.net/alnitak/M8weS/

The key is the .andSelf() function which merges the result of the previous two chained calls into one list.

0

精彩评论

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