仅针对同一页面锚链接



从任何给定网站上的Web分析师的角度来看,我正在查找通过控制台中记录锚的文本值(Inner 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>

将导航到页面上元素的锚定记录。

$(document).ready(function() {
   var anchor = $("a[href^=\#]");
   var url = $(window);
   anchor.on("click", function(e){
     if ( $('#' + $(this).text()).length ) {
          console.log($(this).text());
     }
   });
})
.section {
   height: 400px;
}
.section:nth-child(even) {
   background: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="section" id="one">
	<a href="#one">one</a>
</div>
<div class="section" id="two">
	<a href="#two">two</a>
</div>
<div class="section" id="three">
	<a href="#three">three</a>
</div>
<div class="section" id="four">
	<a href="#four">four</a>
</div>

var anchor = $("a[href^=\#]").not("a[href=#shr-pg0]");
   anchor.on("click", function(){
      var anchorText = this.text;
      $(window).one("hashchange",function() {
        console.log(anchorText);
     });
  });

相关内容

  • 没有找到相关文章