如何删除锚点的onclick事件?

时间:2014-02-20 15:57:21

标签: javascript jquery html

我想删除onclick属性但不工作我使用jQuery但onclick事件不删除这里是我的代码:

 function  getBusinesses(page){
                if(page==0){
                    alert("you are already on First Page");
                    $("#previous a").removeAttr("onclick");
                }
                else{
                    $.ajax
                    ({
                        type: "POST",
                        url: "getbusiness.php",
                        data: "page="+page,
                        success: function(msg)
                        {
                            $("#new2").html(msg);

                        }
                    });
                }
            }
 <div id="new34"  style=" width:1028px; height:15px; background#fff; float:left;">
                       <div id="previous" style="float:left; width:20px;">

<a onclick="getBusinesses(1)" href="javascript:void(0)">&lt;</a></div>

3 个答案:

答案 0 :(得分:0)

如果您尝试停止点击事件,请尝试以下操作...

$('#yourElement').on('click', function(){
    return false
});

答案 1 :(得分:0)

因为您正在执行getBusinesses(1),所以$("#previous a").removeAttr("onclick");代码永远不会有效,因为您将其包含在if(page==0){内 将if更改为if(page<=1){,它将按预期工作。

答案 2 :(得分:0)

我认为代码最糟糕的部分是onclick标记内的<a>属性。

由于您在代码中使用了jQuery,因此您确定应该使用它来将单击处理程序绑定/取消绑定到DOM元素,而不会使用那些混乱的html属性。

这里如何触发你的功能

$("#previous a").click(function(e){

     // this is so it prevents the default 
     // behavior of an anchor redirecting to href
     e.preventDefault
     // triggers the function. I imagine you have a 
     // pagesLeft counter that will count down
     getBusinesses(pagesLeft)

})

当页面左移0

时,现在修改你的JS功能来取消手柄
var pagesLeft = 3 // any you need, you get from somewhere    

function  getBusinesses(page){

    if(page==0){
         alert("you are already on First Page");
         // See here i use jQuery .unbind('click')
         $("#previous a").unbind('click');
    }
    else{
        pagesLeft--       
        $.ajax({
            type: "POST",
            url: "getbusiness.php",
            data: "page="+page,
            success: function(msg)
            {
                 $("#new2").html(msg);

             }
        });
    }
}

HERE IS A WORKING FIDDLE

修改

作为一个整体,您可以看到完整的更改

<div id="new34"  style=" width:1028px; height:15px; background#fff; float:left;">
    <div id="previous" style="float:left; width:20px;">
        <a href="#">&lt;</a><!-- see here no onclick and href="#" -->
    </div>
</div>

var pagesLeft = 3 // any you need, you get from somewhere    

function  getBusinesses(page){

    if(page==0){
         alert("you are already on First Page");
         // See here i use jQuery .unbind('click')
         $("#previous a").unbind('click');
    }
    else{
        pagesLeft--       
        $.ajax({
            type: "POST",
            url: "getbusiness.php",
            data: "page="+page,
            success: function(msg)
            {
                 $("#new2").html(msg);

             }
        });
    }
}

$("#previous a").click(function(e){

     // this is so it prevents the default 
     // behavior of an anchor redirecting to href
     e.preventDefault
     // triggers the function. I imagine you have a 
     // pagesLeft counter that will count down
     getBusinesses(pagesLeft)

})

最后编辑

如果您想保留原始代码的样子,虽然它有一些主要漏洞

<div id="new34" style=" width:1028px; height:15px; background#fff; float:left;">
    <div id="previous" style="float:left; width:20px;">
       <!-- how do you set getBusinesses(1) with 1 as argument? -->
       <a onclick="getBusinesses(1)" href="javascript:void(0)">aaa</a></div>

<script> 
    function  getBusinesses(page){
         if(page==0){
             alert("you are already on First Page");
             $("#previous a").removeAttr("onclick");
         }
         else{
              // Here you should decrement the argument of getBusinesses
              // but how do you do it?
              $("#previous a").attr("onclick","getBusinesses(0)");
              $.ajax
              ({
                  type: "POST",
                  url: "getbusiness.php",
                  data: "page="+page,
                  success: function(msg)
                  {
                      $("#new2").html(msg);  
                  }
              });
          }
      }
 </script>

HERE THE FIDDLE FOR THIS LAST EDIT