css 悬停时更改文本,然后返回到上一文本

2mbi3lxu  于 2023-02-10  发布在  其他
关注(0)|答案(9)|浏览(163)

我有一个评论系统,每个评论都有一个按钮,通常显示回复的数量。我希望当用户将鼠标悬停在按钮上时,文本从“3个回复”变为“回复!",然后,当鼠标离开按钮时,文本返回到“3个回复”。
因为每条评论的回复数量都不一样,所以我不能做一个简单的mouseover/mouseout脚本,一个解决方法是把回复数量作为变量传递,然后创建一个小函数来处理它,但是肯定有更简单的方法,我试着用css中的:hover来改变标签的内容(用css属性content),但是还没有成功。
任何帮助感激,谢谢!

mznpcxlj

mznpcxlj1#

是的,你可以使用CSS content。要在普通文本和"回复!"之间切换,请将普通文本放在span中,并在悬停时隐藏它。

button {
  width: 6em
}

button:hover span {
  display: none
}

button:hover:before {
  content: "Reply!"
}
<button><span>3 replies</span></button>

编辑:这在ie8下可以工作,但在它的兼容模式下不行,所以我假设ie7已经过时了。这会有问题吗?

envsm3lx

envsm3lx2#

我认为这是一个简单的方法。在你的按钮中使用两个范围,一个是内容'x replies',一个是内容'Reply!'。使用CSS和:hover,你可以切换哪个范围显示在悬停状态。

button .comment {
  display: none;
}

button:hover .replies {
  display: none;
}

button:hover .comment {
  display: inline;
}
<button>
    <span class="replies">5 Replies</span>
    <span class="comment">Reply!</span>
</button>

这在任何事情上都很有效,因为它使用非常基本的东西来实现同样基本的目标。

0md85ypi

0md85ypi3#

我将使用jQuery .hover()和jQuery .data():的组合
http://jsfiddle.net/5W4Bd/
超文本:

<div id="myDiv">default text</div>

浏览器:

$('#myDiv')
    .data('textToggle', 5)
    .hover(function (e) {
        var that = $(this);

        // get the text from data attribute
        var textToSet = that.data('textToggle');

        // save the current text so it can be reverted
        that.data('textToggle', that.text());

        // set the new text
        that.text(textToSet);
    }, function (e) {
        var that = $(this);

        // get the text from data attribute
        var textToSet = that.data('textToggle');

        // save the current text so it can be reverted
        that.data('textToggle', that.text());

        // set the new text
        that.text(textToSet);
    });

edit:可以随意重构用作两个hover回调的匿名函数,因为它们完全相同:)

f8rj6qna

f8rj6qna4#

$('#button_id').hover(
    function(){
        $(this).text('Reply!');
    },
    function(){
        $.ajax({
            url: 'script.php',
            type: 'post',
            data: comment_id,
            success: function(num_replies){
                $('#button_id').text(num_replies + ' replies');
            }
        });
    }
);

我认为你可以使用这种函数,当你停止悬停时,你给 AJAX 调用你的评论id,它返回该评论的回复数,你可以决定如何存储/检索你的评论id。

smdnsysy

smdnsysy5#

另一种方法是使用按钮创建两个范围,并为每个范围创建类。

<button>
  <span class="replies">3 Replies</span>
  <span class="comments"></span>
<button>

将第二个空着,在css中,写上:

button{ 
  //your decorative code here }

button:hover .replies{
   display: none; }

button:hover .comments:before{
   content:"Reply!"; }

这与第一个答案类似,但这个答案并不总是适用于SASS或LESS这样的预处理器。

wfauudbj

wfauudbj6#

在现代浏览器中,你也可以使用**:after**伪元素来改变悬停时的文本。

.label             { width:5em }
.label:after       { content:'Hello'; }
.label:hover:after { content:'World'; }
<button class="label"></button>
46scxncf

46scxncf7#

如果有人想在菜单链接上尝试相同的操作,(悬停时的不同语言文本):

span {
  font-size: 12px;
}

a {
  color: green;
}

a:hover span {
  display: none;
}

a:hover:before {
  color: red;
  font-size: 24px;
  content: "ಕನ್ನಡ";
}
<a align="center" href="#"><span>kannada</span></a>
suzh9iv8

suzh9iv88#

可能很简单:

$('.controls button.filter').hover(function(){

    var original = $('#current_filter').text();
    var descr = $(this).attr("title");

    $('#current_filter').html( descr );

}, function() {

    $('#current_filter').text(original);
});
v9tzhpje

v9tzhpje9#

最好改变Html中的内容。

.contact .contact-number {
    opacity: 0;
    position: absolute;
    transition-duration: .3s;
}
.contact:hover .contact-text {
    opacity: 0;
    transition-duration: .3s;
}

.contact:hover .contact-number {
    opacity: 1;
    transition-duration: .3s;
}
.contact-number,
.contact-text {
    position: absolute;
    left: 18px;
    top: 15px;
}
<div class="contact">
  <span class="contact-text">contact us</span>
  <span class="contact-number">9981010848</span>
</div>

相关问题