2010-11-28 72 views
0

給定一個p元素,像這樣: <p>Something</p>我想,當過它的用戶移動鼠標,我們有,而不是<p><a href="blabla.org">go here</a></p>jquery簡單交換p元素?

徘徊,如果鼠標離開P區後,返回到上一個: <p>Something</p>狀態。

我能舉一個這樣的簡單例子嗎?

非常感謝, MEM

+0

謝謝大家......我在這裏與相關Ajax調用的一些問題...嗯...我稍後會試一試。非常感謝您的代碼示例,我將盡我所能。 K.問候。 – MEM 2010-11-28 05:10:00

回答

2

或肯Redler的原始使用.data()保留的東西跟蹤一個簡單的修改:

$('p#someID').hover( 
    function() { 
     var $this = $(this); 
     $this.data('orig', $this.html()).html('<a href="blabla.org">go here</a>'); 
    }, 
    function() { 
     var $this = $(this); 
     $this.html($this.data('orig')).removeData('orig'); 
    } 
); 

http://www.jsfiddle.net/ambiguous/FhET2/1/

1

更新時間:作爲@Phrogz所指出的,我錯過了問題意識的一部分。拍攝時與閉合或使用data()的建議是好的,但這裏的另一種方式(與運動部件的一個有趣的號碼):

$(document).ready(function(){ 
    $('p#someID').hover( 
    function() { // on mouseEnter 
     $(this) 
     .contents() 
     .wrap('<span class="hide"/>') // wrap 'n' hide 
     .end() // back to 'this' 
     .append('<a href="blabla.org">Fascinating Link!</a>'); // add link 
    }, function() { // on mouseLeave 
     $(this) 
     .find('a') 
     .remove() // kill the anchor 
     .end() // back to 'this' 
     .find('span.hide') // the wrapper 
     .contents() // the wrapped 
     .unwrap(); // kill the wrapper, leaving its contents 
    }); 
}); 

這是假定風格像這樣:

span.hide { 
    display: none; 
} 

更新例如:http://www.jsfiddle.net/redler/HAGzH/1/

+0

我相信請求是用錨點替換原始文本,然後恢復原始文本。 (關閉內容或`.data()`似乎是合適的。) – Phrogz 2010-11-28 05:07:52

+0

Phrogz,你說得對。我正在更新。 – 2010-11-28 05:18:25

+0

咦?所以你可以使用/不使用關閉標籤來結束跨度?我的意思是像``..?那是我的新聞... – ClarkeyBoy 2010-11-28 05:40:11

0
$('p#IDHERE').hover( 
    function(){ // on mouseEnter 
     $(this).contents().replaceWith('<a href="blabla.org">go here</a>'); 
    }, 
    function(){ // on mouseLeave 
     $(this).contents().replaceWith("something"); 
    } 
); 

這將替換所有文字