我有一个块:ID搜索只适用于第一个元素
<div class="col-1-4 local_links">
<table>
<tr>
<td>
<a href="#user_profile">User Profile</a><div class="arrow-selected"></div>
</td>
</tr>
<tr>
<td>
<a href="#dashboard">Dashboard</a> </td>
</tr>
<tr>
<td>
<a href="#change_password">Change Password</a>
</td>
</tr>
</table>
</div>
[...一些代码...]
<div class="col-3-4 local_responses">
<div class="content full" id="user_profile" style="display: block;">
<h2>settings :: User Profile</h2>
</div>
<div class="content full" id="dashboard">
<h2>settings :: Dashboard</h2>
</div>
<div class="content full" id="change_password">
<h2>settings :: Change Password</h2>
</div>
</div>
有的JS其中如下:
var local_links = $(".local_links");
var local_responses = $(".local_responses");
$(("a"),local_links).on("click", function(e){
e.preventDefault();
var id = $(this).attr("href");
local_links.find("div.arrow-selected").remove();
$(this).parent().append('<div class="arrow-selected"></div>');
$(".content", local_responses).animate({opacity: 0});
$(id, local_responses).animate({opacity: 1});
});
如果我做了console.log(id)...它很好地显示了ID ......但它只适用于第一个元素。我知道我错过了一些微不足道的东西。有什么想法吗?
ID在页面中必须是唯一的,请改为使用类。 – Esailija
它是独一无二的。 :) –
你确定你不是指'local_links.on(“click”,“a”,function(){});'?你的代码在哪里有一个元素具有类“local_links”? – Ian