jQuery替换悬停时元素的文本
使用jQuery,我试图替换这些悬停链接中的文本,包括span。然后,当用户将鼠标悬停在关闭位置时,将再次显示原始文本jQuery替换悬停时元素的文本,jquery,hover,replace,Jquery,Hover,Replace,使用jQuery,我试图替换这些悬停链接中的文本,包括span。然后,当用户将鼠标悬停在关闭位置时,将再次显示原始文本 <a class="btn" href="#"> <img src="#" alt=""/> <span>Replace me</span> please </a> <a class="btn" href="#"> <img src="#" alt=""/> &
<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>
您可以将原始文本保存在节点本身中存储的
数据defaultText
属性中,这样可以避免变量
$(function(){
var prev;
$('.btn').hover(function(){
prev = $(this).text();
$(this).text("I'm replaced!");
}, function(){
$(this).text(prev)
});
})
您需要将其存储在变量中,以便将其设置回原来的状态,请尝试:
var text;
$(".btn").hover(function () {
text = $(this).text();
$(this).text("I'm replaced!");
},
function () {
$(this).text(text);
});
向悬停事件添加另一个函数,如下所示:
$('.btn').hover(function(){
$(this).text("I'm replaced!");
}, function() {
$(this).text("Replace me please");
});
对于demo来说,如果所有要替换的内容都在某个元素中,那么这会容易得多。您需要用jQuery完成还是静态文本?你应该比我早43秒发布,但有趣的是,当我点击发送时,我的互联网瞬间瘫痪,当我亲自发送时,我不得不输入验证码,这将替换它,但当鼠标熄灭时,它仍然保留新文本!请注意,html节点中的键必须是kebab大小写的,在本例中:
哇,现在我意识到我有多慢了。。。而JSFIDLE链接在这里是个坏主意:'(
$(function(){
var prev;
$('.btn').hover(function(){
prev = $(this).text();
$(this).text("I'm replaced!");
}, function(){
$(this).text(prev)
});
})
$('.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);
});
$('.btn').hover(function(){
$(this).text("I'm replaced!");
}, function() {
$(this).text("Replace me please");
});