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

报告相同问题?

悬赏问题

  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭