简体   繁体   English

如何禁用div元素内的锚标记onclick事件?

[英]How to disable anchor tag onclick events inside div element?

I have html content as given below. 我有如下所示的html内容。

<div>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
</div>

Problem: 问题:

When I click on any link, in the onClick function I will make one ajax call. 当我单击任何链接时,在onClick函数中,我将进行一个ajax调用。

I just want to disable all the onClick events of a tag(or disable the entire div and all of its child elements) till I get the ajax response after getting the response I need to re-enable all the a tag onclick events. 我只想禁用标记的所有onClick事件(或禁用整个div及其所有子元素),直到获得响应后得到ajax响应,我需要重新启用所有标记onclick事件。

Is there any way achieve the same? 有什么办法可以达到同样的效果吗?

You can use beforeSend callback of jqXHR where you can disable all anchor tag and jqXHR.always() callback where you will enable all anchor tag . 您可以使用jqXHR的beforeSend回调(在其中您可以禁用所有锚标记)和jqXHR.always()回调(在其中您可以启用所有锚标记)。

go through Jquery Ajax Documentation 查看Jquery Ajax文档

 <a href="#" onclick="someFunction();">Link 1</a>

someFunction(){
    //some code here
   $.get('test.aspx', function(data){
   //do something here
   })
 return false;
}

Check This DEMO 检查此演示

You can use .ajaxStart() & .ajaxStop() JQUERY events to do this. 您可以使用.ajaxStart().ajaxStop() JQUERY事件来执行此操作。 You can create a different css to disable the anchor tag as below : 您可以创建另一个CSS来禁用锚标记,如下所示:

$('a[href]').each(function () {
                        var $this = $(this);
                        if ($this.attr('href') && $this.attr('href') != '') {
                            $(this).attr('data-href', $(this).attr('href'));
                            $(this).removeAttr('href');
                        }
                    });

and while enabling it back 同时启用它

$('a[data-href]').each(function () {
                var $this = $(this);
                if ($this.attr('data-href') && $this.attr('data-href') != '') {
                    $(this).attr('href', $(this).attr('data-href'));
                    $(this).removeAttr('data-href');
                }
            });

This will remove your href and create a custom attribute. 这将删除您的href并创建一个自定义属性。 So your click event will not work on anchor tag. 因此,您的点击事件不适用于锚标记。

You can try this: 您可以尝试以下方法:

1) Add class to div: 1)将类添加到div:

<div class="myDiv">
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
</div>

2) Hide div when ajax starts and show div in success of ajax: 2)在ajax启动时隐藏div,并在ajax成功时显示div:

    $(".myDiv").css('opacity','0.25');// blur div when ajax starts
    $.ajax("test.php", {
          success: function(data) {
             $(".myDiv").css('opacity','1');// display div as ajax complete
          },
          error: function() {
            alert('An error occurred');
          }
       });
    });

set a flag for active status 为活动状态设置标志

function someFunction(){
   var status = $('#parentDiv').attr('data-active');
   if(!status){
       $("#parentDiv").attr('data-active',true);
        //make ajax call
       $.ajax("url", {
          success: function(data) {
              $("#parentDiv").attr('data-active',false);//make false when done
          }
       });
    }
  }

<div id="parentDiv" data-active="false">
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
    <a href="#" onclick="someFunction();">Link 1</a>
</div>

Note: This is an alternate solution. 注意:这是一个替代解决方案。 IMHO kedar kamthe's solution is the best way to solve this problem 恕我直言kedar kamthe的解决方案是解决此问题的最佳方法

Thanks for so many solutions. 感谢您提供这么多解决方案。

I have found a very simple solution. 我找到了一个非常简单的解决方案。

Solution: 解:

You can use the CSS property pointer-events to disable the click event on any element: 您可以使用CSS属性的pointer-events禁用任何元素上的click事件:

https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events https://developer.mozilla.org/zh-CN/docs/Web/CSS/pointer-events

// To disable:    
document.getElementById('id').style.pointerEvents = 'none';
// To re-enable:
document.getElementById('id').style.pointerEvents = 'auto';
// Use '' if you want to allow CSS rules to set the value

Thanks, 谢谢,

Dixit 迪克西特

you can use beforeSend to set a callback function to disable all elements. 您可以使用beforeSend设置回调函数以禁用所有元素。 and enable them in success. 并使他们成功。 You can add a function to disable/enable those element. 您可以添加一个功能来禁用/启用这些元素。 add an id to the div. 向div添加一个ID。

$.ajax( 
  url: "your url",
  beforeSend: function( xhr ) {
    disabledFields(true);
  },
  success: function(){
    disabledFields(false);
  },
  error: function(){
    disabledFields(false);
  });

//
function disabledField(isDisabled)
{
   $("#myDiv").find("a").each(function(){
        if (isDisabled)
        {
            $(this).attr("disabled", "disabled");
        }
        else
        {
           $(this).removeAttr("disabled");
        }
   });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM