简体   繁体   English

jQuery显示/隐藏栏取决于CSS类

[英]jQuery show/hide bar depending on css class

I have a dynamic number of images with a delete button. 我有一个带有删除按钮的动态图像。 the first time i click a delete button the image is selected and a bar at the top appears. 第一次单击删除按钮时,将选择图像并在顶部显示一个栏。 if i click the same button again, the image is unselected and the top bar should disappear again. 如果我再次单击同一按钮,则该图像将被取消选择,并且顶部栏将再次消失。 the same should happen with multiple selections /removals of selection. 多次选择/删除选择时也会发生同样的情况。

I tried to achieve this by adding a class on click (selected) and removing it when clicked again. 我试图通过在单击(选中)上添加一个类并在再次单击时将其删除来实现此目的。

I then count the the length of the class (how many exist) and if equals 0, the top bar should disappear again. 然后,我计算班级的长度(存在的班级数量),如果等于0,则顶部栏应再次消失。

the console.log shows me that it always counts 0. console.log向我显示它始终计数为0。

what am i missing? 我想念什么?

 var bar = $('#bar'); var deleteBtn = $('.delete'); var selected = $('.selected'); var selection = 0; deleteBtn.click(function() { if ($(this).hasClass("selected")) { $(this).removeClass("selected"); } else { $(this).addClass("selected"); } var selection = selected.length; if (bar.hasClass("hide")) { bar.addClass("show"); } else { if (selection === 0) { bar.removeClass("barShowY"); } } console.log(selection + " selected images"); }); 
 #bar { height: 100px; width: 100%; background-color: firebrick; color: white; } .hide { margin-top: -100px; } .show { margin-top: 0; } .delete { width: 50px; height: 50px; background: yellow; cursor: pointer; } .selected { border-bottom: 3px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bar" class="hide">delete selected images?</div> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Ll2fJdBTPQylZO0Z4JQ9rtsIJsojwBxn.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/lYG2Mgzz878Z8rU3yvlx9MwmR5dhDsyy.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Zp2VXCJxT764bqDPyyIy0Yf2ZRFHzr3g.png" alt=""> 

You should use : 您应该使用:

 var selection = $('.selected').length;

Instead of : 代替 :

var selection = selected.length;

Since the expression var selected = $('.selected'); 由于表达式var selected = $('.selected'); will save all the elements with selected class in selection variable when there's no elements with this class, that why it'll remain with count zero. 当此类中没有元素时,会将所有具有选定类的元素保存在selection变量中,这就是为什么它将保留为零的原因。

 var bar = $('#bar'); var deleteBtn = $('.delete'); var selection = 0; deleteBtn.click(function() { if ($(this).hasClass("selected")) { $(this).removeClass("selected"); } else { $(this).addClass("selected"); } var selection = $('.selected').length; if (bar.hasClass("hide")) { bar.addClass("show"); } else { if (selection === 0) { bar.removeClass("barShowY"); } } console.log(selection + " selected images"); }); 
 #bar { height: 100px; width: 100%; background-color: firebrick; color: white; } .hide { margin-top: -100px; } .show { margin-top: 0; } .delete { width: 50px; height: 50px; background: yellow; cursor: pointer; } .selected { border-bottom: 3px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bar" class="hide">delete selected images?</div> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Ll2fJdBTPQylZO0Z4JQ9rtsIJsojwBxn.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/lYG2Mgzz878Z8rU3yvlx9MwmR5dhDsyy.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Zp2VXCJxT764bqDPyyIy0Yf2ZRFHzr3g.png" alt=""> 

you are selecting the .selected class before applying the class on the element that is the main cause of error, move it inside the delete button see below 您正在选择.selected类,然后将其应用到导致错误的主要原因的元素上,请将其移到删除按钮内,如下所示

 var bar = $('#bar'); var deleteBtn = $('.delete'); var selection = 0; $(document).ready(function() { deleteBtn.on('click', function() { console.log($('.selected').length); if ($(this).hasClass("selected")) { $(this).removeClass("selected"); } else { $(this).addClass("selected"); } var selection = $('.selected').length; if (bar.hasClass("hide")) { bar.addClass("show"); } else { if (selection === 0) { bar.removeClass("barShowY"); } } console.log(selection + " selected images"); }); }) 
 #bar { height: 100px; width: 100%; background-color: firebrick; color: white; } .hide { margin-top: -100px; } .show { margin-top: 0; } .delete { width: 50px; height: 50px; background: yellow; cursor: pointer; } .selected { border-bottom: 3px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bar" class="hide">delete selected images?</div> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Ll2fJdBTPQylZO0Z4JQ9rtsIJsojwBxn.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/lYG2Mgzz878Z8rU3yvlx9MwmR5dhDsyy.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Zp2VXCJxT764bqDPyyIy0Yf2ZRFHzr3g.png" alt=""> 

I've modified the following and I seem to be getting the results you're expecting 我已经修改了以下内容,而且似乎得到了您期望的结果

var selection = $('.selected').length;
if (bar.hasClass("hide")) {
  bar.removeClass("hide");
  bar.addClass("show");
} else {

  if (selection === 0) {
     bar.removeClass("show");
     bar.addClass("hide");
  }
}

Was barShowY supposed to be just show ? barShowY应该只show吗?

Codepen here : https://codepen.io/anon/pen/dZQNxM 这里的Codepen: https ://codepen.io/anon/pen/dZQNxM

selected should be declared in the event handler. selected应该在事件处理程序中声明。 This should be your JS file: 这应该是您的JS文件:

var bar = $('#bar');
var deleteBtn = $('.delete');
var selection = 0;

deleteBtn.click(function() {

  if ($(this).hasClass("selected")) {
    $(this).removeClass("selected");
  } else {
    $(this).addClass("selected");
  }

  var selected = $('.selected'); // Note these two lines
  var selection = selected.length;

  if (bar.hasClass("hide")) {
    bar.addClass("show");
  } else {
    if (selection === 0) {
      bar.removeClass("barShowY");
    }
  }

  console.log(selection + " selected images");
});

Check it out in CodePen https://codepen.io/anon/pen/zPMNeX?editors=1111 在CodePen https://codepen.io/anon/pen/zPMNeX?editors=1111中查看
or on JSBin http://jsbin.com/cifekojasu/edit?js,console,output 或在JSBin http://jsbin.com/cifekojasu/edit?js ,控制台,输出

Your collecting the amount of selected images when the document first loads. 您在首次加载文档时收集所选图像的数量。 Therefore you'll always get the same number (0) due to caching. 因此,由于缓存,您将始终获得相同的数字(0)。 You'll have to recapture the amount on every click so it can live update. 您必须重新获得每次点击的金额,以便实时更新。

 var bar = $('#bar'); var deleteBtn = $('.delete'); deleteBtn.click(function() { var selected; $(this).toggleClass('selected'); selected = $('.selected').length; if (!selected) { bar.removeClass('show'); } else { bar.addClass('show'); } console.log($('.selected').length + " selected images"); }); 
 #bar { height: 100px; width: 100%; background-color: firebrick; color: white; } .hide { margin-top: -100px; } .show { margin-top: 0; } .delete { width: 50px; height: 50px; background: yellow; cursor: pointer; } .selected { border-bottom: 3px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="bar" class="hide">delete selected images?</div> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Ll2fJdBTPQylZO0Z4JQ9rtsIJsojwBxn.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/lYG2Mgzz878Z8rU3yvlx9MwmR5dhDsyy.png" alt=""> <div class="delete">delete</div> <img src="https://apollo2.dl.playstation.net/cdn/UP2131/CUSA01736_00/Zp2VXCJxT764bqDPyyIy0Yf2ZRFHzr3g.png" alt=""> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM