How to toggle HTML text of an anchor tag using jQuery? I want an anchor that when clicked the text alternates between Show Background & Show Text as well as fading in & out another div. This was my best guess:

$(function() {
    $("#show-background").click(function () {
        $("#content-area").animate({opacity: 'toggle'}, 'slow'); 
    });

    $("#show-background").toggle(function (){
        $(this).text("Show Background")
        .stop();
    }, function(){
        $(this).text("Show Text")
        .stop();
    });
});

推荐答案

对不起,问题出在我身上!这是不同步的,但这是因为我的HTML文本的方式不对.在第一次点击时,我希望div淡出,文本显示为"Show text".

Will check more thoroughly next time before I ask!

My code is now:

$(function() {
  $("#show-background").toggle(function (){
    $("#content-area").animate({opacity: '0'}, 'slow')
    $("#show-background").text("Show Text")
      .stop();
  }, function(){
    $("#content-area").animate({opacity: '1'}, 'slow')
    $("#show-background").text("Show Background")
      .stop();
  });
});

Thanks again for the help!

Jquery相关问答推荐

bootstrap - Select Select 器不能在下拉菜单中 Select

如何在for循环期间上传文件

将元素附加到每个函数 jQuery

什么是不使用 jQuery 的经验技术原因?

JQuery手风琴自动高度问题

使用 jQuery 从 JavaScript 对象中添加/删除项目

javascript 正则表达式用于包含至少 8 个字符、1 个数字、1 个大写和 1 个小写的密码

通过 AJAX 和 jQuery 使用 HTML5 文件上传

带有 jquery.animate() 的 CSS 旋转跨浏览器

如何在新窗口中打开链接?

如何隐藏 Twitter Bootstrap 下拉菜单

jQuery:如何创建一个简单的叠加层?

jquery可排序占位符高度问题

带有回调ajax json的jQuery自动完成

jQuery位置href

使用 jQuery 获取鼠标单击图像的 X/Y 坐标

使用 jQuery Select 空文本输入

如何删除集中的 contenteditable pre 周围的边框?

是否可以将 async:false 设置为 $.getJSON 调用

如何在 jquery 中获取 textarea 的值?