简体   繁体   English

定位到定位标记后,页面跳回到顶部

[英]Page is Jumping back to Top after targeting the anchor tag

Hello guys im using wordpress and i need to jump to the comments when the user is clicking a link. 大家好,我正在使用wordpress,当用户单击链接时,我需要跳转到注释。 The single.php is opening in a new tab and its loading the page on the anchor (comment-id) but after that its always jumping back to the top of the page. single.php在新选项卡中打开,并将页面加载到锚点(comment-id)上,但此后始终跳回到页面顶部。

I Know its a problem with javascript (as its working well when i disable js) but im not sure where to find the peace off javascript i have to stop or to change on single.php. 我知道它与javascript有关的问题(当我禁用js时,它能很好地工作),但是我不确定在哪里可以找到javascript的安全源,我必须停止或更改single.php。 Has anyone an idea how and where to change the javascript. 有谁知道如何以及在何处更改javascript。 So that i can stay at the comment after clicking the link on a different page? 这样,我可以在其他页面上单击链接后留在评论中?

this is the target <a name="comment'.$comment_ID.'" href="#comment'.$comment_ID.'" onclick="deletco('.$comment_ID.')">DELETE</a> 这是目标<a name="comment'.$comment_ID.'" href="#comment'.$comment_ID.'" onclick="deletco('.$comment_ID.')">DELETE</a>

and this is the link form the other page to the target on single.php 这是另一页链接到single.php上目标的链接

<a href="'.$commpostlink.'#comment-'.$commentid.'" target="_blank">'.$titlecomm.'</a> 

thanks for any help 谢谢你的帮助

First I encourage you to comment out your JS files one by one it test it out tell you find the responsible js file. 首先,我鼓励您逐个注释掉JS文件,然后对其进行测试,告诉您找到负责任的js文件。

Another approach is, if its javascript issue, then you can do this with jQuery: 另一种方法是,如果出现JavaScript问题,则可以使用jQuery:

$('a.aCommentLinkCssClass').click(function () {
  var url = $(this).attr('href').text();
  window.location.href = url;
});

and add css class to your link: 并添加CSS类到您的链接:

<a href="'.$commpostlink.'#comment-'.$commentid.'" target="_blank" class="aCommentLinkCssClass">'.$titlecomm.'</a> 

or if you want just JavaScript only: 或者,如果您只想使用JavaScript:

<script>
   function goToFunction(url) {
      window.location.href = url;
   }
</script>

and in your html: 并在您的html中:

 <?php echo '<a href="'.$commpostlink.'#comment-'.$commentid.'" onclick="goToFunction("'.$commpostlink.'#comment-'.$commentid.'")" target="_blank">'.$titlecomm.'</a>'; ?>

try: 尝试:

onclick="deletco('.$comment_ID.'); return false;"

Or inside the function itself add a return false; 或在函数本身内部添加return false; at the end. 在末尾。 You need to prevent bubbling of the event. 您需要防止事件冒泡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM