URL中的PHP变量和锚点

时间:2013-08-13 23:57:21

标签: php html

我找不到一个可靠的答案,反复试验让我无处可去。

我正在生成一个动态PHP页面,其中包含我的URL中的变量,因此我的URL看起来像:

http://www.mypage.com/myfile.php?l=1&d=1&c=1

(works)

我还想为此添加一个锚点,例如:

http://www.mypage.com/myfile.php?l=1&d=1&c=1#anchor1
(Does not jump to the anchor at the end)

主播的目标是一个div,例如:

<a href = "http://www.mypage.com/myfile.php?l=1&d=1&c=1#anchor1">Some link</a>
<div id = "anchor1"></div>

这可能吗?

3 个答案:

答案 0 :(得分:2)

这应该效果更好

<a href="http://www.mypage.com/myfile.php?l=1&d=1&c=1#anchor1">Some link</a>
<div>
    <a name="anchor1"></a>
</div>

请注意,正确的方法取决于您使用的doctype。您可以在html5 herehtml 4.01 here

中详细了解相关信息

答案 1 :(得分:0)

某些浏览器不会根据网址哈希自动定位您的id。您可以考虑使用JavaScript。

var doc = document;
function E(e){
  return doc.getElementById(e);
}
function offset(element){
  var x = 0, y = 0, e = element;
  while(e && !isNaN(e.offsetLeft) && !isNaN(e.offsetTop)){
    x += e.offsetLeft - e.scrollLeft;
    y += e.offsetTop - e.scrollTop;
    e = e.offsetParent;
  }
  return {left: x, top: y};
}
function YourSolution(listId){
  var parentId = E(listId), pcn = parentId.childNodes;
  for(var i=0,l=pcn.length; i<l; i++){
    if(pcn[i].nodeType === 1){
      var ac = pcn[i].childNodes;
      for(var n=0,m=ac.length; n<m; n++){
        var an = ac[n];
        if(an.nodeType === 1){
          an.onclick = function(){
            var el = offset(this);
            scrollTo(el.left, el.top);
          }
        }
      }
    }
  }
}

将上述内容放在名为scroll.js的外部JavaScript页面上。现在将以下代码放在body

的底部
  <script type='text/javascript' src='scroll.js'></script>
  <script type='text/javascript'>
    YourSolution('listId');
  </script>
</body>
</html>

答案 2 :(得分:0)

我想问题是id是动态添加的。

然后,您可以在添加ID并将元素附加到文档

后刷新哈希值
var h = location.hash;
location.hash = '';
location.hash = h;

演示http://jsfiddle.net/wpMDj/1/show/#anchor

代码http://jsfiddle.net/wpMDj/1/


或者,您可以使用scrollIntoView

document.getElementById(location.hash.substring(1)).scrollIntoView(true);

但为了避免错误,你需要一些ifs:

var hash = location.hash.substring(1);
if(hash){
    var el = document.getElementById(hash);
    if(el && el.scrollIntoView){
        el.scrollIntoView(true);
    }
}

演示http://jsfiddle.net/wpMDj/3/show/#anchor

代码http://jsfiddle.net/wpMDj/3/