根据jQuery针对特定项目
Target specific item in Accordion with jQuery
我想从另一个页面(例如page.html#secondItem
)链接到手风琴中的特定面板。我已经读过我必须使用location.hash
,但不确定在这个例子中如何使用。
有人能给我建议吗。
演示:http://jsbin.com/macamorasi/1/edit?html,css,js,输出
$(document).ready(function($) {
$('#accordion').find('.accordion-toggle').click(function(){
//Expand or collapse this panel
$(this).next().slideToggle('fast');
//Hide the other panels
$(".accordion-content").not($(this).next()).slideUp('fast');
});
});
.accordion-toggle {cursor: pointer;}
.accordion-content {display: none;}
.accordion-content.default {display: block;}
<div id="accordion">
<h4 class="accordion-toggle" id="firstItem">Accordion 1</h4>
<div class="accordion-content default">
<p>Cras malesuada ultrices augue molestie risus.</p>
</div>
<h4 class="accordion-toggle" id="secondItem">Accordion 2</h4>
<div class="accordion-content">
<p>Lorem ipsum dolor sit amet mauris eu turpis.</p>
</div>
<h4 class="accordion-toggle" id="thirdItem">Accordion 3</h4>
<div class="accordion-content">
<p>Vivamus facilisisnibh scelerisque laoreet.</p>
</div>
</div>
http://jsbin.com/macamorasi/1/edit?html,css,js,输出
您可以检查location.hash
是否存在。如果是,请使用它来查找感兴趣的元素,然后将其向下滑动。然后,您可以使用.siblings()
查找其他<h4>
元素,并获取它们的下一个邻居并将它们向上滑动。
$(document).ready(function($) {
// General accordion click toggle
$('#accordion').find('.accordion-toggle').click(function(){
//Expand or collapse this panel
$(this).next().slideToggle('fast');
//Hide the other panels
$(".accordion-content").not($(this).next()).slideUp('fast');
});
// Check for location hash
if(location.hash) {
// Remove the first '#' character
var hash = location.hash.substr(1);
// Expand element
if($('#'+hash).length) {
$('#'+hash)
.next()
.slideDown()
.end()
.siblings('h4')
.next()
.slideUp();
}
}
});
请参阅此处的完整代码:http://jsbin.com/bonozoqezo/1/edit.要获得正确的演示,请使用哈希访问全屏演示:http://jsbin.com/bonozoqezo/1#secondItem
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 学生搜索项目jquery/javascript
- 无法在jquery中向上或向下移动列表中的多个项目
- 使用jQuery插入HTML页面的第一个项目缺少结束标记
- 使用JavaScript或jQuery检查单选按钮选择是否与数组中的项目匹配
- JQuery和Javascript没有'在我的项目中不起作用,但在网络上起作用
- jQuery:根据select选项中的每页项目进行分页
- 我在页面中使用的jQuery UI可排序项目;不起作用
- 如何使用jquery将所选项目从一个下拉组列表(optgroup)移动到另一个下拉列表(optgroup)
- jQuery无限循环,动画化许多项目
- 如何让li项目在这个jQuery列表中滑动和/或淡出视图
- 比较两个数组并仅显示匹配的项目 Jquery
- 如何选择不包含类/id 的项目 - jquery.
- 使用复选框删除本地内存中存储的项目Jquery
- 如何在MVC上的两个列表框之间移动项目-JQuery不起作用
- Rails项目:jQuery插件在刷新前未加载
- 在数组中保存输入并显示所有项目jquery javascript
- 追加多个项目Jquery
- 如何通过点击图像在下拉列表中更改所选项目(jquery)
- 从列表中删除项目(JQuery)