window.location.href=window.location.href;非常怪异的代码,但确实有作用的,不信往下看:   ^-^ javascript学习不深,希望高手不要见笑 ^-^ 是一个文章评论系统,这是用户评论列表页:
其中的cid387,cid154等锚点是在文章页面里每条评论列表的锚点,要实现“点击每条评论的链接,直接定位到用户发表的评论上”
<table width="100%" border="0" cellspacing="2" cellpadding="4">
  <tr>
    <td width="82%"><a href="./6699.shtml#cid387" target="_blank">楼上专业,太专业了~~...</a></td>
    <td width="16%">2010-02-20 11:07:48</td>
  </tr>
  <tr>
    <td><a href="./431177.shtml#cid154" target="_blank">太专业了,不懂,华丽的飘过...</a></td>
    <td>2010-02-03 15:25:07</td>
  </tr>
  <tr>
    <td><a href="./431213.shtml#cid152" target="_blank">我是来灌水的...</a></td>
    <td>2010-02-03 15:14:12</td>
  </tr>
  <tr>
    <td><a href="./432038.shtml#cid148" target="_blank">好文,顶,收藏了...</a></td>
    <td>2010-02-03 14:09:07</td>
  </tr>
  <tr>
    <td><a href="./432146.shtml#cid145" target="_blank">楼上怎么疯狂灌水?要道德...</a></td>
    <td>2010-02-03 13:31:32</td>
  </tr>
</table>
链接xxx.shtml,靠ajax载入评论内容列表,
$('.comment-article').load(/comment.php?id='+id+'&url='+url);
comment.php?id=xx中的评论列表形式如下<div id="comment_box">
<DIV class=contentrow >
  <P class=cntt><A name=cid146></A>拼图游戏,不必当真。</P>
  <DIV class=clear></DIV>
  <DIV class=u><SPAN class=b>四川省成都市网友</SPAN>&nbsp; <SPAN class=gray>2010-02-03     13:47:33</SPAN> </DIV>
</DIV>
<DIV class=dvd></DIV>
<DIV class=contentrow>
  <P class=cntt><A name=cid150></A>事实是不是这样,还很难讲 </P>
  <DIV class=clear></DIV>
  <DIV class=u><SPAN class=b>上海市网友</SPAN>&nbsp; <SPAN class=gray>2010-02-03     14:59:25</SPAN> </DIV>
</DIV>
<DIV class=dvd></DIV>
<DIV class=contentrow >
  <P class=cntt><A name=cid153></A>10亿年后?眼下就是2012了......</P>
</DIV>
</div>
comment.php?id=xx中的加入如下的javascript$("#comment_box").ready(function(){
//...
window.location.href=window.location.href;
//....
}这样就可以实现定位,xxx.shtml载入完成后,评论的锚点还没有出现在html中,当然无法定位到评论的位置;
而在comment.php?id=xx载入完成后,浏览器并不会自动定位到锚点位置,所以这时要加上这行非常怪异的代码
window.location.href=window.location.href;

解决方案 »

  1.   

    window.location.href=window.location.href;这个是刷新当前页面用的,跟锚点好像没有关系吧
    你加了<a name=cid146></a>这样的锚点了么?
      

  2.   

    没有刷新当前页面的,如果刷新了当前页面,就永远不能实现页面锚定到ajax后载入的锚点了。
    window.location.href=[新地址]
    如果新地址与原地址相同,页面并不会刷新,仅仅是重新定位一下锚点。
    (上页的代码,是从程序页面里摘出来的,并不完整,仅仅是说明问题之用)
    <a name=cid146></a>这样的锚点就是ajax载入的评论列表里的锚点
      

  3.   

    如果当前url中不包含锚点链接的话,页面就会重复刷新了其实这个实现最好用
    window.location.hash=xx实现;