如何使用jquery获取当前URL,或者更具体地说,获取末尾的ID呢?
例如,我有一个product.php#tab-2。我想要从中获取的只是'#tab-2'部分。
我尝试过'window.location.pathname',但这只会返回'/product.php'。
谢谢。
如何使用jquery获取当前URL,或者更具体地说,获取末尾的ID呢?
例如,我有一个product.php#tab-2。我想要从中获取的只是'#tab-2'部分。
我尝试过'window.location.pathname',但这只会返回'/product.php'。
谢谢。
你不需要使用jQuery来实现这个功能:
alert(window.location.href); // will give you the full url
alert(window.location.hash); // will give you the hash (#) value
请查看 Mozilla 文档,网址为 window.location - MDC。
你需要使用 window.location.hash
要将值分配给变量,请使用以下方法;
var url = window.location.href;
var id = url.substring(url.lastIndexOf('#') + 1);