hurriedly% 2018-07-18 03:19 采纳率: 100%
浏览 36

Ajax on Click事件[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/27670401/using-jquery-this-with-es6-arrow-functions-lexical-this-binding" dir="ltr">Using jQuery $(this) with ES6 Arrow Functions (lexical this binding)</a>
                            <span class="question-originals-answer-count">
                                (5 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/20279484/how-to-access-the-correct-this-inside-a-callback" dir="ltr">How to access the correct `this` inside a callback?</a>
                            <span class="question-originals-answer-count">
                                (10 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-07-18 03:46:34Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I am using ajax to redirect pages without refreshing for a tags. However, it randomly works. Sometimes, it doesnt even generate log that I put inside the function on purpose to check if its called. I guess a tag performs href that is already declared on inline. How do I make this 100% work?

$(".view-about-us a").on('click', event=>{

event.preventDefault();
var new_url = $(this).attr("href");

$.ajax({
    dataType: 'html',
    type: "POST",
    url: new_url,
    data: {

    },
    success: function(result) {
      window.history.pushState(null, null, new_url);

      var htmlString = result;

      var parser = new DOMParser();
      var doc = parser.parseFromString(htmlString, "text/html");
      var parent = doc.querySelector(".main-container");
      var children = parent.childNodes;

      children.forEach(child => console.log(child));

      $(".main-container").html(children);
    },
    error: function(result) {
        alert('error');
    }
});
});
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33744141 2018-07-18 03:33
    关注

    One thing you could do is make all of you href attributes javascript:void(0), then store what had previously been your href in a data attribute, such as data-href.

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示