删除默认<href>操作

Removing the default <a href> action

本文关键字:操作 href 默认 删除      更新时间:2023-09-26

当有人点击某个<a>时,有没有办法阻止页面加载下一页标签,相反,我希望它给出 href 值,例如"www.google.com",然后执行 jQuery .load()是这样的

$(".window").load(hrefvalue);

我知道我可以更改所有 href 值来启动 JavaScript 函数,但这需要一点时间,所以我只是在寻找最简单的方法。

所以我希望它做

  1. 单击<a href时停止页面加载下一部分。
  2. 获取 href 值,例如 http://www.google.com。
  3. 然后对 href 页面执行 jquery (.load()$.ajax() ) 调用。

这应该让你开始:

$(document).on('click', 'a', function(event) {
  event.preventDefault();    // Now the link doesn't do anything
  var href = this.href;      // The link's URL is in this variable
});

可以做类似的事情

$(document).on('click', '*[href]', function(e) {
   // Whatever you're trying to do
   e.preventDefault();
   return false;
});
$(document).on('click','a[href]',function(){
    var href = $(this).attr('href');
    $.ajax(href,function(data){
        $(your_container).html(data);
        //data is the HTML returned
        //note that ajax is bound to the
        //same origin policy
        //any request outside your domain won't work
    })
    return false;
});
<a href="javascript:void(0);">...</a>

之后,您可以为此锚点编写脚本。

这应该可以做到:

$("a").click(function(event) {
  event.preventDefault();
  var href = $(this).attr("href");
  $.ajax({
     url: href,
     success: function(data) {
      alert('Load was performed.');
    }
  });
});