【发布时间】:2018-02-08 03:51:29
【问题描述】:
从任何给定网站的网络分析师的角度来看,我正在寻找通过控制台在控制台中记录锚的文本值(内部 html 文本)来找出哪些相同的页面锚被点击最多。
我采取的方法是在每次锚点点击后获取当前页面 url,并检查它们是否在 url 字符串的末尾有任何散列更改,如果是,则在控制台中打印该锚点的文本值。
var anchor = $("a[href^=\\#]");
anchor.on("click", function(){
if("onhashchange" in window) {
var anchorText = this.text;
console.log(anchorText);
}
});
我编写的代码有问题,因为它返回页面上任何锚点的内部 Html 文本(带有外部链接的锚点除外),但我只想跟踪那些指向同一部分的页。
我采取的方法是否需要修改才能开始?
这是我正在使用的一些 html 以及我想要跟踪和不想跟踪的不同锚点。
<!-- Where the click on the anchor i want to target happens -->
<nav>
<h2 class="wb-inv">Promotions</h2>
<ul class="toc lst-spcd col-md-12">
<li class="col-md-4">
<a href="#story1" class="list-group-item">Anchor for section 1</a>
</li>
</nav>
<!-- below is the section of the same page the anchors would point to -->
<div class="col-md-12 pull-left">
<h2 id="story1">Section 1 Title </h2>
<p>Random paragraph 1</p>
</div>
<!-- The html I'm working with contains a set of tabs that reveal information as you click them, the href attribute and the id are both in the anchor. This is an example of anchors I wouldn't want to track-->
<a tabindex="-1" id="details-panel1-lnk" role="tab" aria-selected="false" aria-controls="details-panel1" href="#details-panel1">
<h2 class="h5 mrgn-tp-sm mrgn-bttm-sm">Online</h2>
</a>
【问题讨论】:
-
if ('onhashchange' in window)不检查事件是否发生,只是检查浏览器是否支持该类型的事件。
标签: javascript jquery anchor hashchange