如何从文档 URL 获取哈希

How to get hash from document url

本文关键字:获取 哈希 URL 文档      更新时间:2023-09-26

如果我使用:

jQuery(document).ready(function() {
      console.log($(this));
});

然后在控制台中我有对象:

[Document mypage.html#weather]

我怎样才能获得最后一个 ID?在此示例中,这是 #weather。我想在选择器中使用警报 #weather,例如$(data_from_console + '.add').val();

是否要获取最后一个具有 ID 属性的元素?

㞖:

$(document).ready(function () {
    console.log($('body *[id]').eq(-1));
});

编辑仔细观察,您是否正在寻找哈希标签?韦普如果您的 URL mypage.html#weather您想要 #weather?

在这种情况下,请尝试:

$(document).ready(function () {
    console.log(document.location.hash);
});

你可以使用 last() 方法。试试这个:

$(document).ready(function() {
    $('body *').each(function() { //selects all elements in body which got id
      var lastEle = $(this).last();   //selects last one of them
      console.log(lastEle.attr('id'));//returns it's id to console log
    });
});