jQuery如果URL哈希,请单击事件/激活javascript [英] jQuery if url hash, click event/activate javascript
问题描述
因此从主页上,我有一个链接,该链接转到产品列表页面. 产品页面具有展开/折叠div.
So from the home page, i have a link that goes to a products listing page. The product page has expand/collapse divs.
我需要适当的div来扩展,具体取决于url#是什么.
I need the appropriate div to expand depending on what the url# is.
所以主页上的链接是
<a href="/out-products.php#healthysnacks">healthy snacks</a>
当我单击上面的链接时,我正在尝试在产品页面上激活它:
when i click the link above, i am trying to activate this, on the product page:
<a href="javascript:ReverseDisplay('products4')" id="healthysnacks"> Healthy Snacks</a>
我尝试了一些其他代码,这些代码通过检查哈希标签来触发点击,但它们均无法正常工作,我认为这是由于ReverseDisplay js所致. 请任何见识会有所帮助.
I've tried some of the other codes that i found that trigger click by checking for hash tag and none of them were working properly, i think it's because of the ReverseDisplay js. Please any insight would help.
谢谢
推荐答案
您可以在产品页面的文档准备功能中进行以下更改:
You can make the following changes in the document ready function of your product page:
简单修复::由于jQuery id选择器为#elementId
,因此您可以简单地将window.location.hash
值用作id选择器,并使用它来定位所需的元素.
Simple fix: Since the jQuery id-selector is #elementId
, you can simply use the window.location.hash
value as your id selector, and use it to target the desired element.
if ( window.location.hash ) {
$(window.location.hash).click(); //clicks on element specified by hash
}
更好:除了上述内容之外,还可以将js移出标记.
Better: In addition to the above, take the js out of your markup.
$('#healthysnacks').click(function(e) {
e.preventDefault();
ReverseDisplay('products4');
});
然后,完成此操作后,使用上方的$(window.location.hash).click()
代码.
另外,将链接更改为:
Then, after doing this, use the $(window.location.hash).click()
code from above.
Also, change your link to:
<a href="#" id="healthysnacks"> Healthy Snacks</a>
这篇关于jQuery如果URL哈希,请单击事件/激活javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!