【问题标题】:About HTML and attribute href关于 HTML 和属性 href
【发布时间】:2017-03-21 00:53:00
【问题描述】:

大家晚上好,我的href属性有问题,因为我不知道如何访问文件夹,我举个例子:

我留在 root/store/products/details.php

我想去root/index.php

我的回答是:我如何到达 root/index.php ?我一直在尝试使用 ../../index 但没有奏效

【问题讨论】:

  • 必须是相对URI吗?为什么不直接使用href="root/index.html"
  • @GoojajiGreg 没有前导 / 将转到 root/store/products/root/index.html

标签: php html href attr


【解决方案1】:

如果 root 是您域中的基本路径,例如example.com/root,那么如果您在example.com/root/store/products/details.php,您将有一个如下所示的链接来访问您的根目录。这个href 实际上可以在任何页面上工作。如果您使用../../index.php,它只能在某些页面上工作。

<a href="/root/index.php">Root</a>

【讨论】:

  • 我用过它,但在 WAMPSERVER 中他将我发送到 localhost/index.php 但我想要 localhost/Machinery/index.php ,但请 TY 为您提供帮助
  • @AlejandroFernandez 所以应该是&lt;a href="/Machinery/index.php"&gt;Root&lt;/a&gt;
  • @AlejandroFernandez 这是在子目录中开发很痛苦的原因之一。花几个小时学习如何设置虚拟主机,从长远来看,它会让你的生活更轻松,
【解决方案2】:

这就是我使所有模板链接可移植的方式。

对于在服务器端处理的文件,您将使用 SERVER_ROOT 链接,对于 -a href=...' 中的链接,您将使用 SITE_ROOT

define('SERVER_ROOT', dirname( __FILE__ ) . DS);

$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ?
    'https://' : 'http://') . $_SERVER['SERVER_NAME'];

if (strlen( $url ) <= 10) $url = null;    // For IDE Support

define( 'SITE_ROOT', $url . DS);          // The base URL

所以要实现上面的

<a href="<?=SITE_ROOT?>example/morestuff/">Post Score</a></li>

【讨论】:

    【解决方案3】:

    使用&lt;a href="../../index.php"&gt;

    演示片段:

    var base = 'https://example.com/store/products/details.php'
    
    var relative = '../../index.php'
    
    
    var target = new URL(relative, base).toString()
    
    console.log(target)

    【讨论】:

    • 这很奇怪,我用过它并工作过 Jajajaja,谢谢你的朋友
    【解决方案4】:

    PHP 默认为 index.php,因此您可以链接到 <a href="/root/">Link text</a>

    通过在 URL 前加上 /,例如“/root/”而不是“root/”,您会强制它从您的虚拟主机的根目录或使用 VPS 读取网站的目录。

    如果你自定义了DirectoryIndex,你可以做&lt;a href="/root/&lt;Your Directory Index&gt;.php"&gt;Link&lt;/a&gt;

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 2014-10-21
      • 2012-06-08
      • 2017-01-27
      • 2012-02-05
      • 1970-01-01
      • 2013-07-17
      • 2012-04-28
      • 1970-01-01
      相关资源
      最近更新 更多