gpt4 book ai didi

javascript - 使用 JavaScript 将主题标签附加到 URL

转载 作者:IT王子 更新时间:2023-10-29 03:18:48 24 4
gpt4 key购买 nike

我想在不牺牲 SEO 的情况下构建一个 ajax 网站。我的问题是:如果我的页面上有这样的链接:

   <a href="http://example.com/cats" id="cats">Cats</a>
<a href="http://example.com/dogs" id="dogs">Dogs</a>

...单击每个链接时,我想用相应的主题标签更新地址栏。因此,如果单击“Cats”链接,当前位置将为 http://example.com/#cats我可以用它来显示我的 ajax 内容。如果 javascript 关闭或用户是搜索引擎,他们将直接转到/cats

最佳答案

您可以更改 location.hash 属性,它会更改当前 anchor 标识符而无需离开页面,例如您可以:

<a href="http://mysite.com/cats" id="cats" class="ajaxLink">Cats</a>
<a href="http://mysite.com/dogs" id="dogs" class="ajaxLink">Dogs</a>

然后:

$('.ajaxLink').click(function (e) {
location.hash = this.id; // get the clicked link id
e.preventDefault(); // cancel navigation

// get content with Ajax...
});​

关于javascript - 使用 JavaScript 将主题标签附加到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2366481/

24 4 0