简体   繁体   English

Jquery 选择器 - 我怎样才能确保它有效?

[英]Jquery selector - how can I ensure this works?

I have some buttons, labelled logo1 - logo15 respectively.我有一些按钮,分别标记为 logo1 - logo15。

There is another button called 'lets-go' that fires a function based on these buttons being selected - when you click a logo the class 'active'.还有一个名为“lets-go”的按钮会根据这些按钮的选择触发 function - 当您单击徽标时,class 会“活动”。

When there is no logo selected, I would like this button to not be in the DOM - and be hidden.当没有选择徽标时,我希望此按钮不在 DOM 中 - 并被隐藏。 At the moment, the 'active' class for the button brings it's opacity to 1.目前,按钮的“活动” class 将其不透明度设为 1。

I have this jquery statement at the moment.我现在有这个 jquery 声明。

  if (!$('.logo1, .logo2, .logo3, .logo4, .logo5, .logo6, .logo7, .logo8, .logo9, .logo10, .logo11, .logo12, .logo13, .logo14, .logo15').hasClass("active")) {
$('#lets-go').removeClass('active')};  

But it's not working.但它不起作用。

This is an example of one of my logoX buttons:这是我的 logoX 按钮之一的示例:

$('.logo15').on('click', function(e) {
  $('.logo15').toggleClass("active");
  $('#b15').toggleClass('alive');
  $('#b15').toggleClass('zoomTarget');
  $('#b15').toggleClass('dead');
  $('#lets-go').addClass('active');
  $('#popoutLetsGo').addClass('expand');
  $('.instructions-arrow-2').addClass('hide')
});

On click, they apply the class of 'active' to let's go.单击时,他们将“活动”的 class 应用于 go。 But it doesn't remove it, ever.但它永远不会删除它。 Just if you click any of the 15 buttons a new button appears, but if you deselect the button it's still there - and then the next screen is blank.只要您单击 15 个按钮中的任何一个,就会出现一个新按钮,但如果您取消选择该按钮,它仍然存在 - 然后下一个屏幕是空白的。

Can you see why it's not?你能明白为什么不是吗?

I am basically looking for: If none of these classes have the class of active, then make sure this id doesn't have the class of active either.我基本上在寻找:如果这些类都没有活动的 class,那么请确保这个 id 也没有活动的 class。

Consider the following:考虑以下:

if (!$("[class*='logo']").hasClass("active")) {
  $('#lets-go').removeClass('active')};
}

This looks at the Class attribute for a item starting with "logo" , so .logo3 would be one of those elements.这会查看以"logo"开头的项目的 Class 属性,因此.logo3将是这些元素之一。 But you may want to test each one.但你可能想测试每一个。

$("[class*='logo']").each(function(i, el){
  if(!$(el).hasClass("active")){
      $('#lets-go').removeClass('active')};
    }
});

See More:看更多:

You can also use simplified classes to help group selectors.您还可以使用简化的类来帮助分组选择器。 Consider the following.考虑以下。

 $(function() { $(".logo").click(function() { $(".logo.active").removeClass("active"); $(this).addClass("active"); $("#letsgo").prop("disabled", false); }); $("#letsgo").prop("disabled", true); })
 .logo { padding: .4em; border: 1px solid black; border-radius: 3px; margin: 3px; background: #eee; color: #999; }.active { background: white; color: black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>Make a Selection</p> <div class="logo item-1">Logo 1</div> <div class="logo item-2">Logo 2</div> <div class="logo item-3">Logo 3</div> <div class="logo item-4">Logo 4</div> <div class="logo item-5">Logo 5</div> <button id="letsgo">Let's Go!</button>

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

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