例如:
< a href =:8080>查看其他端口< / a>(这个例子没有用,因为它只会把8080当作我想要的字符串导航到)
解决方案如果这可以起作用,那就好了,我不明白为什么不是因为:是URI组件内部用于端口分离的保留字符,因此浏览器可以真实地将其解释为相对于此URL的端口,但不幸的是,它不会并且无法做到这一点。
因此,您需要使用Javascript来完成此操作;
//为事件委托事件,并保存附加一百万个事件给每个锚点 document.addEventListener('click',function(event){ var target = event.target; if(target.tagName.toLowerCase()=='a') { var port = target.getAttribute('href')。match(/ ^ :( \d +)(。*) /); if(port) { target.href = port [2]; target.port = port [1]; } } },false);在Firefox 4中测试
小提琴: jsfiddle/JtF39/79/
更新:修复了将端口附加到网址结尾的问题,并且还增加了对相对和绝对网址的支持将被追加到最后:
< a href =:8080 / test / blah>测试绝对值< /一个> < a href =:7051./test/blah\">相对测试< / a>
Is there a way without Javascript / server-side scripting to link to a different port number on the same box, if I don't know the hostname?
e.g.:
<a href=":8080">Look at the other port</a>(This example does't work as it'll just treat :8080 as a string I want to navigate to)
解决方案It would be nice if this could work, and I don't see why not because : is a reserved character for port separation inside the URI component, so the browser could realistically interpret this as a port relative to this URL, but unfortunately it doesn't and there's no way for it to do that.
You'll therefore need Javascript to do this;
// delegate event for performance, and save attaching a million events to each anchor document.addEventListener('click', function(event) { var target = event.target; if (target.tagName.toLowerCase() == 'a') { var port = target.getAttribute('href').match(/^:(\d+)(.*)/); if (port) { target.href = port[2]; target.port = port[1]; } } }, false);Tested in Firefox 4
Fiddle: jsfiddle/JtF39/79/
Update: Bug fixed for appending port to end of url and also added support for relative and absolute urls to be appended to the end:
<a href=":8080/test/blah">Test absolute</a> <a href=":7051./test/blah">Test relative</a>
更多推荐
超链接中不同端口号的相对URL?
发布评论