2012-07-11 86 views
0

我有以下代碼:如何正確捕捉mouseout事件?

<div class='no_translate'>Not translated</div> 

,並按照鼠標懸停/鼠標移開事件代碼:

  $('.no_translate').mouseover(function() { 
       $(this).empty(); 
       var field="<form method='POST' action=''><input name='a' type='textarea'/></form>"; 
       $(this).html(field); 
      }); 

      $('.no_translate').mouseout(function() { 
       $(this).empty(); 
       $(this).html('Not translated'); 
      }); 

我想簡單的文字轉換成輸入字段和背部。但是有一個小問題:當我將光標移動到一個新字段上時(不要從它出來),那麼這個字段會轉換成簡單的文本並返回很多次,但是我不會從中解脫出來,而且我也不明白這是怎麼回事。請告訴我。先謝謝你。

+0

你需要使用懸停事件。 – Hardik 2012-07-11 07:20:41

回答

0

JSFiddle code example

$('.no_translate').hover(function() 
{ 
    $(this).empty(); 
    var field="<form method='POST' action=''><input name='a' type='textarea'/></form>"; 
    $(this).html(field); 
}, 
function() 
{ 
    $(this).empty(); 
    $(this).html('Not translated'); 
}); 
0
$("element").hover(
    function() { 
     hover code 
    }, 
    function() { 
    hover out code. 
    } 
); 

,如果你有鋼只是問題你的jsfiddle代碼...

+0

請問,你可以使用懸停事件來轉換我的代碼嗎?先謝謝你。 – user1477886 2012-07-11 07:24:36

+0

http://stackoverflow.com/a/11427557/183431 – fedosov 2012-07-11 07:30:47

+1

謝謝fedosov你很快,我只是建立jsfiddle和簡單你做到了:D – Hardik 2012-07-11 07:33:39

0

這裏是工作提琴:http://jsfiddle.net/surendraVsingh/vvbnE/

jQuery代碼:

$('.no_translate').hover(function() { 
       $(this).empty(); 
       var field="<form method='POST' action=''><input name='a' type='textarea'/></form>"; 
       $(this).html(field); 
}, function(){ 
     $(this).empty(); 
     $(this).html('Not translated'); 
}); 
0

試試這個

$(function(){ 
    $('.no_translate').hover(function() { 
     $(this).empty(); 
     var field="<form method='POST' action=''><input name='a' type='text'/></form>"; 
     $(this).html(field); 
    }, 
    function() { 
     $(this).empty(); 
     $(this).html('Not translated'); 
    }); 
}); 

DEMO.