切换按钮和切换可见性 [英] Toggle button and Toggle visibility

查看:96
本文介绍了切换按钮和切换可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此jQuery隐藏DIV:

I'm using this jQuery to hide a DIV:

$("#slider").click(function() {
    $(".help").slideToggle();
    $("#wrapper").animate({ opacity: 1.0 },200).slideToggle(200, function() {
    $("#slider a").text($(this).is(':visible') ? "Hide" : "Show");
    });
});

我不想改变文本显示,而是要根据.toggle

Instead of altering the text show I want to display a graphic, I want the grpahic to change based on the .toggle

当前HMTL:

<div id="wrapper">
  LI ITEMS
</div>

<div class="help">
  IMAGE
</div>

<div id="slider">
  <a href="#">Hide</a>
</div>

我想将两个图像添加到.slider中,删除<a>:

I'd like to add two images into .slider, removing the <a>:

<div id="slider">
<img class="show" title="Hide" alt="Hide" src="images/showbutton.png" />
<img class="hide" title="Hide" alt="Hide" src="images/hidebutton.png" />
</div>

也许我可以使用CSS隐藏隐藏"按钮,然后以某种方式使用jquery进行切换?

Perhaps I can use css to hide the 'hide' button, then switch that using jquery in some way?

有什么建议吗?

推荐答案

最初可以通过CSS隐藏一个,然后只需在函数中切换两个即可,将其用于CSS:

You can hide one initially via CSS, then just toggle the two in your function, use this for CSS:

.hide { dislay: none; } //Reverse if "Hide" should be the default button

这个jQuery使用 .toggle() (不带参数,它将切换可见性):

And this jQuery, using .toggle() (without arguments, it toggles visibility):

$("#slider").click(function() {
  $(".help").slideToggle();
  $("#wrapper").animate({ opacity: 1.0 },200).slideToggle(200, function() {
    $("#slider img").toggle();
  });
});

这将切换两个图像的可见性...由于一个图像被隐藏,而您只有2个,因此有效地交换了它们.还请确保将alt/title属性固定在屏幕阅读器人员的.show上:)

This would toggle the visibility of both images...since one's hidden and you only have 2, it effectively swaps them. Also make sure to fix your alt/title attributes on the .show one for the screen reader folks :)

这篇关于切换按钮和切换可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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