[英]How open my toggle-accordion from another page?
I wrote the toggle-accordion. 我写了切换手风琴。 It works good if links and accordion there are on one page, url links works.
如果链接和手风琴在一页上,则效果很好,URL链接有效。 But how make open my toggle-accordion to url from another page?
但是,如何打开我的切换手风琴以从另一个页面进入URL?
https://codepen.io/malinosky/pen/wxKzEo https://codepen.io/malinosky/pen/wxKzEo
$(function(){
var accordionFaqHead = $('.i-accordionFaq-head');
accordionFaqHead.on('click', function() {
$(this).next().slideToggle();
});
function accHash() {
var hash = $(this).attr('href');
if (hash) {
var accordionItem = $(hash);
if (accordionItem.length) {
accordionItem.find('.i-accordionFaq-body').show();
}
}
}
$('.i-link-hash').on('click', accHash);
});
You can check if there is hash
in the url
and call your function with it: 您可以检查
url
是否包含hash
,并使用它调用函数:
JS: JS:
if (window.location.hash) {
var $hashedElement = $(window.location.hash);
accHash.apply($hashedElement);
}
Use apply
to call your function while setting this
to be equal to $hashedElement
使用
apply
打电话给你的功能,同时设置this
等于$hashedElement
To add hash while going from page to page you need to add it to your anchor tag first: 要在逐页浏览时添加哈希,您需要先将其添加到锚标记中:
HTML: HTML:
<a href="myotherpage.html#item1"></a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.