我有一个页面,其中有 3 个带文本的占位符。这些都隐藏在 page_load 上,jQuery 根据是否有标签将它们设置为可见。
我想使用带有标签的内联链接,例如<a href="#references"></a>
. 根据主题标签,我想显示/隐藏其他内容。
使用查询字符串很容易开始工作,但不幸的是,在我的情况下,这将给搜索引擎优化带来非常糟糕的效果。
我找到了一个可行的解决方案,但有一个主要问题。当用户单击顶部的附加主题标签的菜单时,它会转到链接,但我的 jQuery 在第二次单击时首先运行。会发生以下情况:
- 我们在 URL http://www.domain.com
- 用户点击我的菜单,网址是http://www.domain.com#info,但标记没有任何变化
- 用户第二次点击菜单,url是一样的,但是现在标记改变了
我的 jQuery 脚本:
<script>
$(document).ready(function () {
updateview();
});
function updateview() {
if (document.location.hash.search('calculator') !== -1) {
$("#ContentContainer").hide();
$("#VideoPnl").hide();
$("#CalculatorPnl").show();
}
else if (document.location.hash.search('video') !== -1) {
$("#CalculatorPnl").hide();
$("#VideoPnl").show();
$("#ContentContainer").hide();
} else {
$("#ContentContainer").show();
$("#CalculatorPnl").hide();
$("#VideoPnl").hide();
}
}
我的菜单
我菜单上的每个菜单点都添加了以下 JavaScript,我希望在单击时会改变屏幕,但这个第一次工作第二次:
menu.Attributes.Add("onclick", "javascript:updateview()");
我在 ASP.NET 中的标记:
<asp:Panel runat="server" ID="ContentContainer" ClientIDMode="Static">
<asp:Literal runat="server" ID="ContentPlaceholder"></asp:Literal>
</asp:Panel>
<asp:Panel ID="CalculatorPnl" runat="server" ClientIDMode="Static">
<asp:Literal runat="server" ID="CalculatorHeadlineLtl"></asp:Literal>
<asp:Literal runat="server" ID="CalculatorPlaceholder"></asp:Literal>
</asp:Panel>
<asp:Panel ID="VideoPnl" runat="server" ClientIDMode="Static">
<asp:Literal runat="server" ID="VideoHeadlineLtl"></asp:Literal>
<asp:Literal runat="server" ID="VideoPlaceholder"></asp:Literal>
</asp:Panel>