【问题标题】:how to remove onclick event of anchor? [duplicate]如何删除锚的onclick事件? [复制]
【发布时间】:2014-03-21 16:50:06
【问题描述】:

我想删除 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>

【问题讨论】:

  • 从.html文件中的元素中移除属性,然后使用javascript绑定事件。
  • 您能添加正确的代码吗?我认为它没有正确添加
  • 这不是重复的,因为 OP 使用了正确的方法,但他的代码中有逻辑错误。
  • @arif 您的代码运行良好。你用 0 的 page 的值检查了吗?
  • 你显然有其他事情发生,或者你的问题不清楚。既然你否定了我的回复,那么你可以在这里看到;适合我jsfiddle.net/SpYk3/wUFm6

标签: javascript jquery html


【解决方案1】:

我认为您的代码中最糟糕的部分是 &lt;a&gt; 标记内的 onclick 属性。

既然您在代码中使用了 jQuery,那么您肯定应该使用它来将点击处理程序绑定/取消绑定到 DOM 元素,而不需要那些凌乱的 html 属性。

这里是你如何触发你的功能 ONCLICK

$("#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)

})

现在修改您的 JS 函数以在 pagesLeft RECHES 0 时取消绑定处理程序

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

【讨论】:

    【解决方案2】:

    由于您正在执行 getBusinesses(1),因此您的 $("#previous a").removeAttr("onclick"); 代码将永远无法工作,因为您将其包裹在 if(page==0){ 中 将 if 更改为 if(page&lt;=1){,它将按预期工作。

    【讨论】:

      【解决方案3】:

      如果您试图停止点击事件的工作,也许尝试以下...

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-10
        • 2021-08-15
        • 2023-02-07
        • 1970-01-01
        • 2011-06-22
        • 1970-01-01
        相关资源
        最近更新 更多