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.


    This sets a click event on all anchor links.

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

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

相关推荐 更多相似问题