<ul>
<li>Index 0</li>
<li>Index 1</li>
<li>Index 2</li>
</ul>
<div>
<a>Index 0</a>
&l开发者_开发百科t;a>Index 1 (If i click this this this i want to addClass to it to LI with the same index of this )</a>
<a>Index 2</a>
</div>
You can invoke jQuerys .index()
help method for that purpose. It returns the index relative the the siblings of the current node. To find the li node
with the according index, use .eq()
help
$('a').click(function() {
$('ul li').eq($(this).index()).addClass('your_new_class');
});
Demo: http://www.jsfiddle.net/2rFn3/
Referring to your comment
$('a').click(function() {
$('ul li').eq($(this).index()).addClass('your_new_class').siblings().removeClass('your_new_class');
});
Demo: http://www.jsfiddle.net/2rFn3/1/
$("a").click(function() {
//Remove classes from other li (per comment)
$("ul li").removeClass("class");
//Now update the clicked item
var length = $(this).prevAll().length;
$("ul li:eq(" + length + ")").addClass("class");
});
firstly you can add an id on the div and the li to make it easier to select.
div#start and ul#target
$("div#start > a").bind("click dblclick",function(){
$("ul#target:nth-child("+$(this).index()+")").addClass("whatever");
});
精彩评论