【问题标题】:How do I link to other pages created dynamically?如何链接到动态创建的其他页面?
【发布时间】:2014-10-03 17:57:22
【问题描述】:

我有一个 services.php 页面,我正在使用 for 循环使用 MySQL 数据库中的信息创建页面的不同服务内容。

<?php for ($i=0; $i < $numberOfRecords; $i++) { ?>

<div class="download-div">

    //this line below creates the anchor with the correct id
    <a id="<?= $scroll ?>" name="<?= $scroll ?>"></a>
    <h1><?= $service[$i] ?></h1>
    <p><textarea spellcheck="true" class="service-text" name="description<?= $i ?>"><?= $description[$i] ?></textarea></p>
    <p class="current-file">Current file: <?= $image[$i] ?></p>
    <input type="file" class="browse" name="image<?= $i ?>">
    <div class="img-wide">
        <img src="upload/<?= $image[$i] ?>">
    </div>

</div>

<?php } ?>

循环创建 10 个不同的服务部分,每个部分都有服务名称、描述和图像。

当我点击另一个页面上的锚标签(IE )应该链接到每个部分中a标签的id时,它只是转到services.php 页面的顶部。但是,当我再次单击该链接时,它会转到我希望它转到的页面部分。 (即 )

就好像浏览器在页面加载/创建之前看不到锚ID。然后一旦加载,一旦我点击锚标记,它将转到我想要的页面区域。

有没有办法解决这个问题?

【问题讨论】:

  • 对我来说没有多大意义。
  • 导航栏中的链接 重新造林 不会将您带到 id 为重新造林的页面部分,它只是将您带到页面顶部。但是,当您在 services.php 页面上再次单击它时,它会起作用并将您发送到 id 为 reforestation 的地方
  • 没有逻辑上的理由为什么会发生这种情况,php是服务器端,所以这不应该是一个问题,这里有任何JS吗?你能给我们一个我们可以测试的链接吗?
  • 我目前只是将它托管在本地 wamp 服务器上
  • 经常有同样的问题...锚标签有时有效,然后无效。

标签: php html mysql hyperlink anchor


【解决方案1】:

尝试删除 name= 属性,因为我在某些同时具有这两者的浏览器中遇到了问题,并确保您的文档中没有其他 id="reforestation"(例如在菜单中)它应该转到任何具有匹配 id= 的标签(根据 HTML5 规范)。还要确保您没有滚动或进行任何输入,因为这会破坏滚动操作。检查它在其他浏览器中的作用是否相同。

【讨论】:

    【解决方案2】:

    因为它正在导航到另一个页面。我会在页面上使用 javascript 来检查 Windows 地址栏中是否有哈希。下面的函数可以提取该哈希。

    window.onload = function() {
      if(window.location.hash) {
        var hash = window.location.hash;
        return hash.substring(1); // remove #
        //Once you have this hash you can scroll your page to that element
        location.hash = "#" + hash;
      }
    }
    

    如果你更喜欢上面的函数,你可以使用类似下面的东西

    function scrollTo(hash) {
      location.hash = "#" + hash;
    }
    
    function getHash() {
      var hash = window.location.hash;
      return hash.substring(1); // remove #
    }
    
    window.onload = function() {
      if(window.location.hash) {
        scrollTo(getHash());
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多