问题描述
无论是否以
-
/path/to/page
-
http://mydomain.com/path/to/page
-
https://mydomain.com/path/to/page
/path/to/page
http://mydomain.com/path/to/page
https://mydomain.com/path/to/page
并用
-
/#/path/to/page
感谢您的帮助.
dvhh在评论中指出,更好的解决方案是定位点击次数.很抱歉,我不会测试标记正确答案的解决方案.谢谢您的帮助
As pointed out by dvhh in a comment, a better solution would be to target clicks. So sorry, I won't be testing solutions to mark a correct answer. Thanks for your help
发布了.
推荐答案
您应该可以执行以下操作:
You should be able to do something like this:
$('a').each(function() {
if ( this.host === 'mydomain.com' || this.getAttribute('href').indexOf('/') === 0) {
this.href = "/#" + this.pathname;
}
});
它检查href.host
是否匹配domain.com
或href
的第一个字符是/
,如果是,它将href
设置为/#
加上当前的路径名部分href
.
It checks to see if the href.host
matches the domain.com
or if the first character of href
is /
, and if so it sets the href
to /#
plus the current pathname part of the href
.
我正在使用本机getAttribute()
,因为我认为从获取设置的实际原始属性的角度来看,这是最安全的.也许不会有什么改变.
I'm using the native getAttribute()
because I think it is safest in terms of getting the actual original attribute that was set. Perhaps it wouldn't make a difference.
这篇关于如何在jQuery中选择所有本地链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!