如何在html <a> tag

时间:2016-05-27 14:28:45

标签: javascript php

Let's I have two server running on my PC. server1 and server2....

  1. In server1 there is a html page.(ex. a.html)
  2. In server2 there is another html page.(ex b.html)
  3. Server1 is running on port 80
  4. Server2 is running on port 8080

In a.html of server1 there is a tag. Now I want to access b.html from server2 by the above link that's why I have written

<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.

...... I want to access a.html from server1 in my phone. My pc and phone both are in the same wifi network.

To solve this problem I opened my phone browser and gave my pc ip ex. 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

.... I know where is the problem . The problem is in the link because I explicitly wrote localhost:8080 . Here is the problem. I should write my pc private it instead of localhost then it'll work I know. But how to get my private IP by javascript to modify the link dynamically by javascript.

Help me to solve this problem.

2 个答案:

答案 0 :(得分:1)

您的手机基本上是重定向到自身。

您的<a>标记应如下所示:

192.168.0.1/a.html

中的

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

或者如果您不想弄乱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>

答案 1 :(得分:1)

两台服务器都在您的电脑上运行。因此,要访问除自有PC之外的两台服务器,您需要提供私有IP。这样您就可以访问两台服务器。

现在,您首先要访问服务器1的a.html,其中有一个指向server.html的b.html的链接。

不要显式写 localhost 。 并且您想要动态更改链接的href。 在步骤下面。

  1. 将aql of server1的链接更改为<a id="link" href="#"></a>
  2. 您必须动态更改链接。所以要改变链接我正在使用JQuery window.onload()方法。将此脚本添加到您的代码中
  3. <script>
      window.onload=function()
    {
      //Modify the link href
    	var ip=location.host;
    	$("a#link").attr("href", "http://"+ip+":8080/b.html");
    }
    </script>  

    1. 因为您的server2在端口8080上运行并且有一个名为b.html
    2. 的文件
    3. 现在你不用担心。它可以在您的电脑和同一网络中的另一台电脑或手机上使用。