如果我使用:
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));
});
编辑
仔细一看,您是在寻找哈希标签吗? IE。如果你的 URL 是 mypage.html#weather
你想要 #weather 吗?
在那种情况下尝试:
$(document).ready(function () {
console.log(document.location.hash);
});
关于javascript - 如何从文档 url 获取哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11952020/