将 id 附加到 URI - 怎么做?

Append id to URI - how to?

我对一个元素执行了一些 jquery-toggle-action 操作,并想在 URL 上附加一个静态 ID。这就是我正在做的事情:

$("#trigger").click(function () {
    $("#target").css({display: 'inline-block', transition: '0.5s'});
    var hash = $(this).attr("href","#added-to-the-uri");
    location.hash = hash;
});

但它不会生成: xyz.html#added-to-the-uri 但是这个 xyz.html#[对象对象]

我需要为此编辑什么?谢谢!

你只需要这样写

location.hash = "#added-to-the-uri";

这一行:

var hash = $(this).attr("href","#added-to-the-uri");

您没有将 #added-to-the-uri 设置为 hash 变量。您正在设置 $(this) 的值。属性函数 returns $(this) 对象。

您应该像这样添加锚点:

$("#trigger").click(function () {
  $("#target").css({display: 'inline-block', transition: '0.5s'});
  location.hash = 'added-to-the-uri';
});