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

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?

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).

add +

href='# + ".$dir.'/'.$file."'