jQuery 点击显示再次点击隐藏

<html>
    <head>
        <script type="text/javascript" src="/jquery/jquery.js"></script>
    </head>
    <body>
        <div>
            <span class="color">深咖色</span>
            <div class="cc"></div>
        </div>
        <div>
            <span class="size">11*11</span>
            <div class="bb"></div>
        </div>
    </body>
</html>
            
1 .color,.size{  font-size:14px; color:black;}
2 .cc{ width:300px;height:100px;background:red;}
3 .bb{ width:300px;height:100px;background:red;}

第一种是简单的显示和隐藏,使用了bind()和toggle();

1 $(function(){
2      $(".color").bind("click",function(){ $(this).next(".cc").toggle();});
3      $(".size").bind("click",function(){ $(this).next(".bb").toggle();});
4 });

toggle()方法自己就有显示隐藏的作用。

但是这段js的缺点是,当我点击“深咖色”显示红色方块,再次点击“11*11”时,绿色方块出现,但是红色方块也不会隐藏,如图:

第二种方法:改进上述的缺点,当再次点击时,隐藏其他,只出现点击出现的相应内容。

$(function(){  $(".color").bind("click",function(){
      $(this).next(".cc").show();
      $(".bb").hide();
    });
       $(".size").bind("click",function(){
      $(this).next(".bb").show();
       $(".cc").hide();
   });
  });

第三种,在第二种的基础上添加,当点击显示时,出现相应内容,点击其他消失,或者点击其他空白处消失。

  $(function(){

    $(".color").bind("click",function(){
      $(this).next(".cc").show();
      $(".bb").hide();
    });
       $(".size").bind("click",function(){
      $(this).next(".bb").show();
       $(".cc").hide();
   });

$(".color").on("click", function(e){
    $(document).one("click", function(){
        $(".cc").hide();
      $(".bb").hide();
    });

    e.stopPropagation();
});
$(".cc").on("click", function(e){
    e.stopPropagation();
});

$(".size").on("click", function(e){
    $(document).one("click", function(){
        $(".bb").hide();
    $(".cc").hide();
    });

    e.stopPropagation();
});
$(".bb").on("click", function(e){
    e.stopPropagation();
});
 });

one() 方法为被选元素附加一个或多个事件处理程序,并规定当事件发生时运行的函数。

当使用 one() 方法时,每个元素只能运行一次事件处理器函数。

时间: 2024-08-08 12:48:36

jQuery 点击显示再次点击隐藏的相关文章

iOS 图片点击放大, 再次点击缩小

XWScanImage  是一个自定义的类, 评论留Q 发 ImageView = [[UIImageView alloc]init]; ImageView.backgroundColor = SetColor(160, 160, 160); //为UIImageView1添加点击事件 UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action

jQuery如何实现点击下拉菜单的隐藏和显示

jQuery如何实现点击下拉菜单的隐藏和显示:在众多的网站都有这样的效果,那就是点击一个父菜单就会显示下拉子菜单,再点击父菜单,那么子菜单就会隐藏,下面就简单介绍一下如何实现此中效果.这里只介绍如何使用jQuery实现此效果,暂且不考虑效果的美观度,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" co

jquery 点击显示隐藏的三种方法

jquery点击显示隐藏的三种方法,从复杂到简单.使用jquery需要引用jquery库,如右所示<script src="jquery-1.11.3.min.js"type="text/javascript"></script>. 旁边按钮随着收缩展开发生状态变化,展开+变—,收缩—变+. 收缩效果: 展开效果: 一.HTML结构(盗用网上的) <body> <!-- 收缩展开效果 --><li class=&

jquery点击显示隐藏块

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>点击显示块</title> <script src="script/jquery-2.1.4.js"></script>            //引入jquery模块 <style>          

锋利的Jquery(点击显示隐藏div)

点击显示隐藏div 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text

vue2.0实现点击后显示,再次点击隐藏

描述.点击系统切换,弹出系统切换框.再次点击系统切换,隐藏.点击其他地方.也会隐藏 在layout.vue中写的html代码 1.在main.js中写入全局函数 // 定义全局点击函数,右侧系统切换点击其他地方隐藏系统切换菜单,在layout.vue中使用 Vue.prototype.globalClick = function (callback) { document.onclick = function () { callback(); }; }; 2.在layout.vue中的js部分代

juqery 点击显示点击隐藏,slideDown slideUp slideToggle

<!doctype html> <html> <head> <meta charset="utf-8"> <title>webrx-title</title> <script src="js/jquery-1.11.2.min.js"></script> <style> #ad{ width:200px; height:300px; background-co

listview中Item记录点击状态的方法--(点击显示、隐藏的状态保存)

listView中的Item有时候会添加其他的内容,例如有一块儿隐藏的区域,点击后展开,再次点击则隐藏.这时如果Item超过一屏,那么直接在Adapter中的getView方法加入判空操作,即 if(convertView == null){ convertView = new DownloadItem_CachedView(context); } 这时候,可能会出现混乱显示的情况. 解决办法,有三种. 1.  首先,各位想到的可能是直接把判空操作去掉,那么就不会复用,也就不会出现混乱了.这确实

HTML5--》点击显示隐藏内容

<details>浏览器支持比较差,可以用JavaScript实现这种功能. 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>js点击显示隐藏内容</title> 6 <style type="text/css" > 7 body{font-size:12px;} 8 span{f