2017-12-18 7 views
-2

私はmouseleaveイベントを持っています。私が要素を去るとき、私はマウスが置かれた要素を取り除くことはしません(これが意味をなさない場合)。以下の例と私が試したこと。それはそれがどういう仕組みではないから(私は推測する)、そうすることができないと思いましたが、それを行うにはどのような方法がありますか? Atm。leavedの要素を削除する

$('#wrapper').remove() 

むしろ

+0

あなたがあなたの最初の2つのセクションでは、それだけではない 'を持っているスニペット、に記載されている' .removeを() '、使用しているときあなたのコードが動作します.remove'。 – Herohtar

答えて

1

remove()

$(document).on('mouseleave', '#wrapper', function(e) { 
 
    console.log($(this).remove); 
 
    alert('Mouse leaved and wrapper should be removed'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper">asdcasdcasd</div>

$(document).on('mouseleave', '#wrapper', function(e) { 
    console.log($(this).remove()); 
    alert('Mouse leaved and wrapper should be removed'); 
}); 
以下の方法ではない特性である:私は次のようにセレクタを使用することはできません。使用 remove()ない remove

$(document).on('mouseleave', '#wrapper', function(e) { 
 
    console.log($(this).remove()); 
 
    alert('Mouse leaved and wrapper should be removed'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wrapper">asdcasdcasd</div>

+0

私の場合、#wrapperが追加されたことがあったに違いないでしょう。とにかく私はあなたの答えを受け入れるでしょう、それは削除なしのタイプミスでした:)。 – simon

+0

@simon、あなたのコードから何を達成したいですか?あなたのイベントは '#wrapper'要素にバインドされており、' this'は '#wrapper'を参照しています。 – Mamun