我使用下面的javascript(jQuery)切换打开两个DIV。 DIV打开罚款,一次打开一个,这就是我所追求的。但是,我也希望DIV在第二次点击时关闭,尽管我付出了最大的努力,但这种情况不会发生!
使用Javascript:
$(document).ready(function() {
$('.info_tab').click( function() {
var currentID = $(this).next().attr("id");
$('.toggle_info[id!=currentID]').hide(); /* the intention here is to hide anything that is not the current toggling DIV so as to close them as a new one is opened. I thought this would leave the currently selected DIV uninterrupted to toggle closed (below), but it doesn't */
$(this).next().toggle();
return false;
});
});
HTML:
<div class="info_tab">
<h1>Person One</h1><br />
<p>click for less info</p>
</div>
<div id="person_one_info" class="toggle_info">
<p>More information about Philip Grover</p>
</div>
<div class="info_tab">
<h1>Person Two</h1><br />
<p>click for less info</p>
</div>
<div id="person_two_info class="toggle_info">
<p>More information about Roy Lewis</p>
</div>
如果需要更多信息,请询问,我将很乐意编辑问题。
干杯,
富
答案 0 :(得分:2)
你有这个概念,但没有正确使用“currentID”。你需要记住它是一个变量,如果你想要它被评估,就不能在另一个字符串中。
话虽如此,试试这个:
$('.toggle_info[id!='+currentID+']').hide();
这使变量在选择器中得到评估,然后将其传递给jQuery来查找。
但是,看起来你会得到一个手风琴效果。 jQuery UI有just such a control你可以使用(如果你感兴趣的话)。如果你想要经验,那就继续吧。 ; - )