jQuery-我想单击一个单词将其更改,然后再次单击以将其更改回 [英] JQuery -- I want to click a word to change it then click it again to change it back

查看:86
本文介绍了jQuery-我想单击一个单词将其更改,然后再次单击以将其更改回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够单击您好!"将其更改为再见!"然后再次单击将其更改回"Hello!".我已经尽全力打了招呼!更改为再见!",但我不知道如何使函数重复".

I want users to be able to click on 'Hello!' to make it change to 'Goodbye!' and then click it again to change it back to 'Hello!'. I've got as far as making 'Hello!' change to 'Goodbye!', but I don't know how to make the function 'repeat'.

我是这方面的新手,欢迎所有帮助和建议!

I'm an extreme novice at this, so all help and advice is welcome!

据我所知:

HTML:

<body>
   <p id = "greeting">Hello!</p>
</body>

jQuery:

$(function(){
    $("#greeting").click(function(){
        $(this).text('Goodbye!').toggle(hide);
    });
});

推荐答案

$("#greeting").click(function(){
     $(this).text(function(_, oldText) {
         return oldText === 'Goodbye!' ? 'Hello!' : 'Goodbye!';
     });
});

http://jsfiddle.net/DPeWk/

这篇关于jQuery-我想单击一个单词将其更改,然后再次单击以将其更改回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆