<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<style>
.on{background-color:red;}
.b{width:200px;background-color:green;border:1px solid #000;}
</style>
</head>
<body>
<form id="form1" method="post">
<div class=a><span class="b">aa a</span></div>
<div class=a><span class="b">bb b</span></div>
<div class=a><span class="b">cc c</span></div>
<div class=a><span class="b">dd d</span></div>
<div class=a><span class="b">ee e</span></div>
</form>
</body>
</html>
<script type="text/javascript">
$(function() {
//$("div.a").click(function() {
// alert($(this).index());
//});
$(".b").click(function() {
var i = $(this).index();
alert(i);/////这里怎么都是0
$(".b").removeClass("b").eq(i).addClass("on");
})
});
</script>
index 怎么都是0呢
搜索与参数表示的对象匹配的元素,并返回相应元素的索引值。
如果找到了匹配的元素,从0开始返回;如果没有找到匹配的元素,返回-1。