首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用href并单击侦听器来收集?

如何使用href并单击侦听器来收集?
EN

Stack Overflow用户
提问于 2013-10-16 19:56:07
回答 3查看 95关注 0票数 0

我有一个链接列表,每个链接都有href属性:

代码语言:javascript
复制
<a href="./page1.php" class="points">page 1</a>
<a href="./page2.php" class="points">page 2</a>
<a href="./page3.php" class="points">page 3</a>
<a href="./page4.php" class="points">page 4</a>

我有一位听众听这门课的“要点”:

代码语言:javascript
复制
$(".points").live('click',function(){
    id = $(this).attr("id");
    $.ajax({
        type: "POST",
        url: 'points.php',
        data:{id:id},
        success: function()
        {

        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
            alert(textStatus);
            alert(errorThrown);
            alert(XMLHttpRequest.responseText);
        }
    });
});

href是工作的,但单击不工作,当我移除href属性时,单击侦听器可以工作perfectly.Is,这两种方法都可以工作吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-10-16 20:06:56

Ajax和Href一起工作

试一试

代码语言:javascript
复制
  $(".points").live('click',function(e){
  e.preventDefault();
  var id = $(this).attr("id");
  var url= $(this).attr("href");

   $.ajax({
   type: "POST",
   url: 'points.php',
   data:{id:id},
    success: function()
    {
     //Do something before going to page
       window.location.href=url;

    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {
        alert(XMLHttpRequest);
        alert(textStatus);
        alert(errorThrown);
        alert(XMLHttpRequest.responseText);
    }
   });
   });
票数 0
EN

Stack Overflow用户

发布于 2013-10-16 19:58:20

您需要取消默认行为,因为当您离开时,脚本将停止运行。您可以考虑等待Ajax调用结束,然后通过脚本导航:

代码语言:javascript
复制
$(".points").live('click',function(e){ // <--- Add e parameter

    e.preventDefault(); // <--- Add this

    id = $(this).attr("id");
    href = $(this).attr("href"); // Link we will navigate to when done
    $.ajax({
        type: "POST",
        url: 'points.php',
        data:{id:id},
        success: function()
        {
           location.href = href; // Do the navigation now that we're done
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
            alert(textStatus);
            alert(errorThrown);
            alert(XMLHttpRequest.responseText);
        }
    });
});
票数 2
EN

Stack Overflow用户

发布于 2013-10-16 19:58:43

试试这个:

代码语言:javascript
复制
$(".points").live('click',function(e){ //added e as function argument
    e.preventDefault(); //added preventDefault();
    var id = this.id; // changed here to this.id, and added var (so it will be a local variable)
    $.ajax({
        type: "POST",
        url: 'points.php',
        data:{id:id},
        success: function()
        {

        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
            alert(textStatus);
            alert(errorThrown);
            alert(XMLHttpRequest.responseText);
        }
    });
});
  • 阅读关于preventDefault()的文章
  • 请注意,As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19412580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档