2011-02-27 18 views
2

懸停內容正在被替換,但是當鼠標離開元素時我希望它改回來。我將如何做到這一點?回去之前.text()被使用

$('.img-wrap').hover(function(){ 
     $(this).find('h4').text('Go to site'); 
    });  

回答

3

您需要將原始文本存放在$.data在第一個回調,然後在第二個回調閱讀。

例如:

$('.img-wrap').hover(function(){ 
    var h4 = $(this).find('h4'); 
    h4.data('oldText', h4.text()).text('Go to site'); 
}, function() { 
    $(this).find('h4').text(function() { return $.data(this, "oldText"); });  
}); 
+0

原始文本將被客戶端改變,所以有什麼我可以做什麼?也許複製元素中的任何內容然後重用它? – nowayyy 2011-02-27 20:24:11

+0

@Trippy:你是什麼意思?這正是這個代碼所做的。 – SLaks 2011-02-27 20:25:03

+0

h4.data('oldText'我需要插入舊文本嗎?但是舊文本會在某些時候被人改變。是否有其他方式? – nowayyy 2011-02-27 20:28:35

0

hover看看它是爲mouseentermouseleave

這裏快捷方式是example

+0

這也不是一個答案 – SLaks 2011-02-27 20:15:09

+0

增加了一個例子... – Rafay 2011-02-27 20:19:02

0

你要保存文本則在更換之前:

$('.img-wrap').mouseenter(function(){ 
    var $h4 = $(this).find('h4'); 
    $(this).data("old-text", $h4.text()); 
    $h4.text('Go to site'); 
}). mouseleave(function() { 
    var $h4 = $(this).find('h4'); 
    $h4.text($(this).data("old-text")); 
    $(this).data("old-text", null); 
}); 

祝你好運!

+0

'hover'有什麼問題? – SLaks 2011-02-27 20:15:28

+0

當你進入'.img-wrap'包含的任何元素時,使用'mouseenter'可以防止當你將鼠標移動到元素上時獲得重複的事件。祝你好運!http://api.jquery.com/mouseenter/ – 2011-02-27 20:19:05

+0

@Gonzalo:你錯了。'hover' calls' mouseenter',你描述的是'mouseover',這個'hover'與之無關。 – SLaks 2011-02-27 20:24:42

0

您應該檢查hover-功能的API。 你可以通過一個額外的功能,做到這一點:

$('.img-wrap').hover(function(){ 
    $(this).find('h4').text('Go to site'); 
}, 
function() { 
    $(this).find('h4').text(''); 
}); 
相關問題