質問

I want to unbind all clicks binded to an element via its classess

The elements are like this :

<i  class="icon-edit edit <%=AgentData.isSortiDeService(agent)%>" alt="<%=noteEchelle.getIdNote()%>"></i> //where isSortieDeService is a java method which returns a 

string , the values of this string may be '' or 'isSortiDeService'

given that element like this may have some clicks binded like :

//******************modifier la notation
       $(".edit").live("click",function(){ 
        var idNote = $(this).attr("alt");
         $("#divBody").empty();
         $("#divBody").load("<%=path%>/situationAdministrative/notation/editNote.jsp",{idNote:idNote});
         $("#divTitle").empty();
         $("#divTitle").append('Modifier la note');
         $("#div").css('width','650px');
         $("#div").modal({ dynamic: true });
          });

so i tried this

       $(".isSortiDeService").die('click').unbind("click").off("click").click(function(){
   alert("Cet agent n'est plus en service. Vous ne pouvez plus effectuer cette opération");
    });
  });

the click is not ubinded because it is binded to onther class even if it is the same element .

so i tried onther solution

  $('*').each(function(){
       if($(this).hasClass("isSortiDeService"))
       { $(this).die('click').unbind("click").off("click").click(function(){
       alert("Cet agent n'est plus en service. Vous ne pouvez plus effectuer cette opération");
        });

       }});

this also not working ,and the click is fired

i will express the problem like this : i have an element with multiple class, if my element has a specific class , i want to unbind all the clicks binded to others classes , but apprently unbind or off or die works for the same selector : so the same classe

any suggestion?

役に立ちましたか?

解決

Use the below code if the clicked item has a specific class.

$(document).on('click', function (e) {
 if($(this).hasClass('class')) {
  e.preventDefault();
  e.stopImmediatePropagation();
 }
});

他のヒント

It has to be

$(".isSortiDeService").off('click');

To just remove all the click handlers attached to the element.

Did you try only

Remove all event handlers from element like this?

$(".isSortiDeService").off();

As of jquery 1.7 the use of .live has been deprecated so you perhaps shouldn't use it anymore: http://api.jquery.com/live/

If you bind the click event using .click instead of .live, the element should have the event bound to it and then you can unbind this

Example

Well, this seems to be a workaround:

http://jsfiddle.net/cnu4u/

<div class="test test2"></div>

{ here for delegation used with .on() }

$(document).on('click','.test2',$.noop);
var arrClass = $('.test').attr('class').split(' ');
$.each(arrClass,function(i){
    $(document).off('click','.'+arrClass[i]);
});
console.log($._data(document,'events').click);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top