English 中文(简体)
jjQuery 替换悬盘上元素的文本
原标题:jQuery replace text of element on hover

使用 jQuery 时, 我正试图替换文本, 包括横线, 以在这些盘旋的链接中 。 当用户盘旋时, 原始文本将再次显示 。

<a class="btn" href="#">
    <img src="#" alt=""/>
    <span>Replace me</span> please
</a>

<a class="btn" href="#">
    <img src="#" alt=""/>
    <span>Replace me</span> please
</a>

最终产出应当是:

<a class="btn" href="#">
    <img src="#" alt=""/>
    I m replaced!
</a>

我到处玩,但不知道怎么换回来,有什么想法吗?

$( .btn ).hover(function(){
    $(this).text("I m replaced!");
});
最佳回答
$( .btn ).hover(
    function() {
        var $this = $(this); // caching $(this)
        $this.data( defaultText , $this.text());
        $this.text("I m replaced!");
    },
    function() {
        var $this = $(this); // caching $(this)
        $this.text($this.data( defaultText ));
    }
);

您可以在节点本身存储的 data- defaultText 属性中保存原始文本,以避免变量

问题回答

这应该能做这个把戏

$(function(){
  var prev;    

  $( .btn ).hover(function(){
  prev = $(this).text();
      $(this).text("I m replaced!");
  }, function(){
      $(this).text(prev)
  });
})

将其它函数添加到悬停事件上 :

$( .btn ).hover(function(){
    $(this).text("I m replaced!");
}, function() {
    $(this).text("Replace me please");
});

< a href=" "http://jsfiddle.net/3JDZs/" rel=" no follow" >Link 用于演示

$( .btn ).hover(function() {
    // store $(this).text() in a variable     
    $(this).text("I m replaced!");
},
function() {
    // assign it back here
});

您需要将其存储在一个变量中, 才能将其恢复到原来的状态, 尝试 :

var text;

$(".btn").hover(function () {
    text = $(this).text();
    $(this).text("I m replaced!");
},
function () {
    $(this).text(text);
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签