通过location.hash选择特定的锚元素
我试图通过单击另一个页面上的链接来选择网页的特定选项卡部分。建议我使用 location.hash 函数来选择选项卡的特定锚元素,并在第一个网页的 href 属性中添加哈希属性。
但该代码对我不起作用。我得到整个页面,而不是所选的特定选项卡。有人可以帮我吗?
这是代码。这是第一个网页中的链接。我希望选择第二个网页的“已提交”选项卡。所以我添加了该选项卡的 id,#subscribed 到 url。
<a id="formStatus<?php echo $status;?>" class="code_link" href="/FormBuilder/main/viewAllMyForms#submitted"><?php echo $status;?></a>
这是第二页的代码,我在其中检查 location.hash 是否等于已提交。
if(location.hash=="submitted") {
$("#submitted").trigger("click");
}
$('#submitted , #formStatusSubmitted').click({
<?php foreach($myForms as $form):
if($form['Form']['status']=="Incompleted"){ ?>
$('.fm_myformsample_container'+<?php echo $form['Form']['id'];?>).hide();
<?php }
else{?>
$('.fm_myformsample_container'+<?php echo $form['Form']['id'];?>).show();
<?php }
endforeach;?>
$('#sort_by').find(".selected").removeClass();
$('#submitted').addClass("selected");
});
I'm trying to select a particular tab section of a web page, on clicking a link on another page. I was suggested to use the location.hash function to select that particular anchor element of the tab and add the hash property in the href attribute of the first web page.
But the code doesn't work for me.I get the entire page,instead of the particular tab selected. Can some one help me out?
Here is the code. This is the link in the first web page. I want the Submitted tab of the second webpage to be selected. So I have added the id of that tab, #submitted to the url.
<a id="formStatus<?php echo $status;?>" class="code_link" href="/FormBuilder/main/viewAllMyForms#submitted"><?php echo $status;?></a>
This is the code of the second page,where I check if location.hash equals submitted.
if(location.hash=="submitted") {
$("#submitted").trigger("click");
}
$('#submitted , #formStatusSubmitted').click({
<?php foreach($myForms as $form):
if($form['Form']['status']=="Incompleted"){ ?>
$('.fm_myformsample_container'+<?php echo $form['Form']['id'];?>).hide();
<?php }
else{?>
$('.fm_myformsample_container'+<?php echo $form['Form']['id'];?>).show();
<?php }
endforeach;?>
$('#sort_by').find(".selected").removeClass();
$('#submitted').addClass("selected");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 window.location.hash 或 document.location.hash
try window.location.hash or document.location.hash
在我看来,在设置单击处理程序之前,您会触发 click() 。尝试将
$("#subscribed").trigger("click");
移动到底部。Looks to me like you fire off the click() before you set the handler for click. Try moving
$("#submitted").trigger("click");
to the bottom.我找到了解决问题的方法。我刚刚在 if 条件下向 Id 添加了一个 #,
现在它可以工作了。如果我点击该特定链接,我会被重定向到该特定选项卡部分。
I found the solution to my problem. I just added a # to the Id in that if condition, that is,
and it works now..If I clink on that particular link, I get redirected to that particular tab section.