这是我的小提琴:
http://jsfiddle.net/jamesbrighton/wxWgG/4/
http://jsfiddle.net/jamesbrighton/wxWgG/4/
HTML:
<div> <p class="click">Click 1</p> <p>This should be ignored</p> <p>This should be ignored</p> <p>This should be ignored</p> </div> <div> <p class="target">Target 1</p> </div> <div> <p class="target">Target 2</p> </div> <div> <p class="click">Click 2</p> <p>This should be ignored</p> <p>This should be ignored</p> <p>This should be ignored</p> </div> <div> <p class="target">Target 3</p> </div> <div> <p class="target">Target 4</p> </div>
jQuery的:
$('.click').click(function() { $(this).nextAll('.target').css('color','red'); });
我需要它,所以当你点击一个p.click时,下一个p.target变成红色.
因此,如果您单击“单击1”,则“目标1”将变为红色.如果单击“单击2”,则“目标3”变为红色.
除了.find我尝试过.closest,从jQuery文档中我觉得它应该可行.正如你从HTML中看到的那样,.target不是.click的孩子,以防万一.