0

ボディに mousemove イベントを追加したい要素が 2 つありますが、これらの要素を除外したいと考えています。jQueryでこれを達成するにはどうすればよいですか?

$("body").on("mousemove",function(){
    //dont run following code if mouse is on these elements
})
4

1 に答える 1

1

除外する要素を選択してキャッシュし、オブジェクトの jQuery.index()メソッドとtargetプロパティを使用できeventます。

var $blackList = $('#elem, #elem2, #elem3'),
       timeout = ''; 

$(document).on("mousemove", function(e) {
   // Removing last timout using ID returned by setTimeout(if any) 
   clearTimeout(timeout);  

   // Setting timeout using setTimeout function, 
   // so the handler is executed once during each specified duration   
   timeout = setTimeout(function() {
      // event.target returnes target element of the event
      // if the index of the element in jQuery collection(an array of elements) is -1
      // execute the specified code 
      if ($blackList.index(e.target) === -1) { 
          // ...
      }
   }, 50); // change the duration according to your needs
});
于 2013-09-11T15:56:35.357 に答える