简体   繁体   English

如何在html <a>标记中</a>设置href

[英]How set href in html <a> tag

Let's I have two server running on my PC. 我的PC上运行了两台服务器。 server1 and server2 .... server1server2 ....

  1. In server1 there is a html page.(ex. a.html) 在server1中有一个html页面。(例如a.html)
  2. In server2 there is another html page.(ex b.html) 在server2中有另一个html页面。(ex b.html)
  3. Server1 is running on port 80 Server1正在端口80上运行
  4. Server2 is running on port 8080 Server2在端口8080上运行

In a.html of server1 there is a tag. 在server1的a.html中有一个标记。 Now I want to access b.html from server2 by the above link that's why I have written 现在我想通过上面的链接从server2访问b.html,这就是我写的原因

<a href="localhost:8080/b.html">

It's working really well if I access a.html and click that link working fine in my computer where both servers are running. 如果我访问a.html并单击该链接在我的计算机中正常运行,并且两台服务器都在运行,那么它的工作效果非常好。

...... I want to access a.html from server1 in my phone. ......我想从手机中的server1访问a.html。 My pc and phone both are in the same wifi network. 我的电脑和手机都在同一个wifi网络中。

To solve this problem I opened my phone browser and gave my pc ip ex. 为了解决这个问题,我打开手机浏览器并给了我的电脑ip ex。 192.168.0.1/a.html 192.168.0.1/a.html

It's working fine. 它工作正常。 But as I said in above that there is a link in that page (ex given above). 但正如我在上面所说,该页面中有一个链接(如上所述)。 When I click on that link it's giving error 当我点击该链接时,它给出了错误

This site can't be reached localhost refused to connect 此站点无法访问localhost拒绝连接

.... I know where is the problem . ....我知道问题在哪里。 The problem is in the link because I explicitly wrote localhost:8080 . 问题出在链接中,因为我明确写了localhost:8080 Here is the problem. 这是问题所在。 I should write my pc private it instead of localhost then it'll work I know. 我应该把我的电脑私有而不是localhost,然后我知道它会工作。 But how to get my private IP by javascript to modify the link dynamically by javascript. 但是如何通过javascript获取我的私有IP来通过javascript动态修改链接。

Help me to solve this problem. 帮我解决这个问题。

Your phone is basically redirecting to itself. 您的手机基本上是重定向到自己。

Your <a> tag should look like this: 您的<a>标记应如下所示:

in 192.168.0.1/a.html 在192.168.0.1/a.html

<a href="192.168.0.1:8080/b.html">

or if you prefer not to muddy up your HTML then this also possible 或者如果您不想弄乱HTML,那么这也是可能的

in 192.168.0.1/a.html 在192.168.0.1/a.html

<HTML5>
    <head>
        <base href="192.168.0.1:8080/">
    </head>
    <body>
        <a href="b.html">b.html on server #2</a>
        <br>
        <a href="192.168.0.1/c.html">c.html on server #1</a>
    </body>
</html>

Both servers are running on your pc. 两台服务器都在您的电脑上运行。 So, to access both servers except own pc you need to provide your private ip. 因此,要访问除自有PC之外的两台服务器,您需要提供私有IP。 This way you can access your both servers. 这样您就可以访问两台服务器。

Now, you, at first want to access your a.html of server1 where there is a link to b.html of server2. 现在,您首先要访问服务器1的a.html,其中有一个指向server.html的b.html的链接。

Don't write localhost explicitly. 不要显式写localhost And you want to change href of the link dynamically. 并且您想要动态更改链接的href。 Follw below step. 在步骤下面。

  1. Change your link of a.html of server1 to <a id="link" href="#"></a> <a id="link" href="#"></a> of server1的链接更改为<a id="link" href="#"></a>
  2. You have to change your link dynamically. 您必须动态更改链接。 So to change the link I am using JQuery window.onload() method. 所以要改变链接我正在使用JQuery window.onload()方法。 Add this script to your code 将此脚本添加到您的代码中

 <script> window.onload=function() { //Modify the link href var ip=location.host; $("a#link").attr("href", "http://"+ip+":8080/b.html"); } </script> 

  1. Because your server2 is running on port 8080 and has a file named b.html 因为您的server2在端口8080上运行并且有一个名为b.html的文件
  2. Now you don't need to worry. 现在你不用担心。 it'll work on both your pc and another pc or phone in the same network. 它可以在你的电脑和同一网络中的另一台电脑或手机上工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM