doudao7113
2019-02-16 11:59
浏览 72
已采纳

单击锚点后如何提醒主题标签

I am trying to alert a # tag on click of an anchor which produces the # tag. But I only get an alert after a page refresh:

The anchor:

$folderanchor = "<a class='folderanchor' href='#".$dir.'/'.$file."'>$file</a>"; 
echo $folderanchor;

Reading the #:

$( document ).ready(function() {    
     if(window.location.hash) {
          var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
          alert (hash);
          // hash found
      } else {
          // No hash found
      }
}); 

The alert should appear immediately after clicking the anchor. What am I doing wrong?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douliang6563 2019-02-16 12:56
    已采纳

    A click on an anchor link does not necessarily reload the page. You might want to use an event for this.

    $('a[href^="#"]').click(yourFunction);
    

    This sets a click event on all anchor links.

    Put the code you have, without the document ready part, in the function (yourFunction).

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题