我希望这是一个简单的修复,或者我的逻辑可能是错误的。我正在使用 jquery 创建手风琴菜单。手风琴的头部有单选按钮,机身内部有时间表。I want the user to be able to expand and collapse the accordion, and when a schedule is chosen, the selected schedule will be outlined in red with a light grey background. 我正在工作,它只是将箭头旋转和toggleClass应用于每个元素,而不是选择的单个元素。
当前发生的情况是,当您单击“计划”时,所有箭头都在旋转,而不是单个箭头。Also, when a radio button is selected , it is highlighting every div with the "clickedSchedule" class.
我认为这与在函数中使用 (this) 有关...我只是不确定如何编写它。也许我也没有以优雅的方式执行此操作,因此我对所有建议持开放态度。
HTML:
<ul id="menu" class="dropdown">
<div class="clickedSchedule">
<li><a class="expanded"><div class="arrow"></div><h2>Schedule 1</h2></a>
<div class="scheduleChoice" style="text-align:left;">
<input name="first_choice" id="checkbox_first" type="radio" value="Schedule 1" /><label>First Choice</label>
<input name="second_choice" id="checkbox_second" type="radio" value="Schedule 1" /><label>Second Choice</label>
<input name="third_choice" id="checkbox_third" type="radio" value="Schedule 1" /><label>Third Choice</label>
</div>
<ul>
<li>
<div><img src="images/01_fall.gif" width="550" height="334" /></div>
<div><img src="images/01_sp.gif" width="550" height="334" /></div>
</li>
</ul>
</li>
</div>
<div class="clickedSchedule">
<li><a class="expanded"><div class="arrow"></div><h2>Schedule 2</h2></a>
<div class="scheduleChoice" style="text-align:left;">
<input name="first_choice" id="checkbox_first" type="radio" value="Schedule 2" /><label>First Choice</label>
<input name="second_choice" id="checkbox_second" type="radio" value="Schedule 2" /><label>Second Choice</label>
<input name="third_choice" id="checkbox_third" type="radio" value="Schedule 2" /><label>Third Choice</label>
</div>
<ul>
<li>
<div><img src="images/02_fall.gif" width="550" height="334" /></div>
<div><img src="images/02_sp.gif" width="550" height="334" /></div>
</li>
</ul>
</li>
</div>
</ul>
jQuery
<script>
//Expand/Collapses Menu & Rotates arrow
$(document).ready(function() {
$("#menu:first li > a").click(function() {
$(this).toggleClass("expanded").toggleClass("collapsed").parent().find('> ul').slideToggle("fast");
$("#menu:first li > a > div").toggleClass("arrowUp").toggleClass("arrow");
});
});
</script>
<script>
//highlights chosen Schedule
$(document).ready(function(){
$("input[name$='first_choice']").click(function(){
var radio_value = $(this).attr("checked");
if(radio_value=='checked') {
$("div.clickedSchedule").css({"border-color" : "red", "border-style" : "solid", "border-width" : "1px", "background-color" : "#f6f6f6"});
}
});
});
</script>
CSS:
我有一个切换这些类的 toggleCLass。
.arrow {
background:url(../images/accordionDropArrow_expanded.png) no-repeat;
width:18px;
height:17px;
float:left;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
}
.arrowUp {
background:url(../images/accordionDropArrow_expanded.png) no-repeat;
width:18px;
height:17px;
float:left;
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
}
也许我只是将这些东西强加在一起,并且有一个更优雅的编码解决方案。我会把它留给stackoverflow的神:)
更新
我正在运行最新的 jquery 脚本。如果单击结果中的第一个选项,您会看到它选择了每个 clickedSchedule 类,而不仅仅是一个。