简体   繁体   English

jQuery函数不适用于每个选择器

[英]jQuery function not working for each selector

I am having difficulties for getting my code to work for every selector. 我很难让我的代码适用于每个选择器。 I have made 2 accordions with opening CSS animation. 我已经打开CSS动画制作了2条手风琴。 The issue is that the second accordion doesn't work, and the animation doesn't work separately for both accordions. 问题是第二个手风琴不起作用,并且动画对于这两个手风琴也不能单独工作。 Here is my source: JSFIDDLE 这是我的资料来源: JSFIDDLE

Here is the jQuery im using 这是使用jQuery的即时通讯

    $('#accordion').find('.accordion-toggle').click(function() {

  //Expand or collapse this panel
  $(this).next().slideToggle('slow');

  //Hide the other panels
  $(".accordion-content").not($(this).next()).slideUp('fast');

  if ($('img').hasClass('moved')) {
    $('img').removeClass('moved');
  } else {
    $('img').addClass('moved');
  }
});

The answer comes down to ids are singular. 答案归结为id是单数。 You can not have more than one element with an id. ID不能超过一个元素。 So when you select $('#accordion') it is returning the first element with that id. 因此,当您选择$('#accordion')它将返回具有该ID的第一个元素。

Change it to be an class. 将其更改为类。 Now you also need to change the code so it is looking for the image inside of the accordion instead of all images. 现在,您还需要更改代码,以便它在手风琴中查找图像而不是所有图像。

So change id="accordion" to class="accordion" 因此,将id="accordion"更改为class="accordion"

and do something like this 并做这样的事情

 $('.accordion').find('.accordion-toggle').click(function() { var panel = $(this).next(); //Expand or collapse this panel panel.slideToggle('slow'); //Hide the other panels $(".accordion-content").not(panel).slideUp('fast').parent().find("img").removeClass("moved"); var img = $(this).find('img').toggleClass("moved"); }); 
 .accordion-toggle:hover{ cursor:pointer; } img { width: 30px; position: absolute; transition: all 1s; } .openclose { text-align: center; position: relative; margin: 30px; } .moved { left: calc(50% - 15px) !important; top: 50% !important; transform: rotate(630deg) !important; } .container{ background: grey; border-radius: 20px; padding: 30px 0; width: calc(100% - 30px); margin: auto; } .accordion-content{ padding:10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="container"> <div class="accordion"> <div class="accordion-toggle"> <div class="openclose"> <p class="resp-p man">I am working fine :)</p> <img style="transform: rotate(-90deg); left: calc(50% - 15px); top: calc(100% + 30px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="top: calc(50% - 15px); transform: rotate(180deg); left: 100%;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="left: -30px; top: calc(50% - 15px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="transform: rotate(90deg); left: calc(50% - 15px); top: -60px;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> </div> </div> <div style="display: none;" class="accordion-content default"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> </div> </div> </div> <br> <div style="text-align:center">Lots of ocntent between</div> <br> <div class="container"> <div class="accordion"> <div class="accordion-toggle"> <div class="openclose"> <p class="resp-p man">I dont work :(</p> <img style="transform: rotate(-90deg); left: calc(50% - 15px); top: calc(100% + 30px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="top: calc(50% - 15px); transform: rotate(180deg); left: 100%;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="left: -30px; top: calc(50% - 15px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="transform: rotate(90deg); left: calc(50% - 15px); top: -60px;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> </div> </div> <div style="display: none;" class="accordion-content default"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> </div> </div> </div> 

You are using same ids for two elements. 您对两个元素使用相同的ID。 due to which id selector only selects first match element and not all element with same id. 由于哪个ID选择器仅选择第一个匹配元素,而不是所有具有相同ID的元素。 You can rather use class selector from container div. 您可以使用容器div中的类选择器。

you will also need to target image elements in currently clicked element container to make this work. 您还需要在当前单击的元素容器中定位图像元素以完成此工作。 You can use make your code much cleaner by using toggleClass)_ instead of checking class existence and toggling classes: 您可以通过使用toggleClass)_来使代码更整洁,而不是检查类的存在和切换类:

$('.container').find('.accordion-toggle').click(function() {
 var _this = $(this);
 //Expand or collapse this panel
  _this.next().slideToggle('slow');
 //Hide the other panels
  $(".accordion-content").not(_this.next()).slideUp('fast');
  _this.find('img').toggleClass('moved');
});

Working Demo 工作演示

Try the following code ,replace the id with a class use find() to find the elements relative to the accordion-toggle 尝试以下代码,将id替换为类use find()来查找相对于手风琴切换的元素

   $('.accordion').find('.accordion-toggle').click(function() {

  //Expand or collapse this panel
  $(this).next('.accordion-content').slideToggle('slow');

  //Hide the other panels
  $(".accordion-content").not($(this).next()).slideUp('fast');//slide up all acordeons except this one
   $('img').not($(this).find('img')).removeClass('moved');//remove all moved classes exept for the current clicked one

    $(this).find('img').toggleClass('moved');// toggle the current class

});

demo: https://jsfiddle.net/03r1vmfv/2/ 演示: https : //jsfiddle.net/03r1vmfv/2/

 $('.accordion').find('.accordion-toggle').click(function() { //Expand or collapse this panel $(this).next().slideToggle('slow'); //Hide the other panels $(".accordion-content").not($(this).next()).slideUp('fast'); $('img').not($(this).find('img')).removeClass('moved'); $(this).find('img').toggleClass('moved'); }); 
 .accordion-toggle:hover { cursor: pointer; } img { width: 30px; position: absolute; transition: all 1s; } .openclose { text-align: center; position: relative; margin: 30px; } .moved { left: calc(50% - 15px) !important; top: 50% !important; transform: rotate(630deg) !important; } .container { background: grey; border-radius: 20px; padding: 30px 0; width: calc(100% - 30px); margin: auto; } .accordion-content { padding: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="container"> <div class="accordion"> <div class="accordion-toggle"> <div class="openclose"> <p class="resp-p man">I am working fine :)</p> <img style="transform: rotate(-90deg); left: calc(50% - 15px); top: calc(100% + 30px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="top: calc(50% - 15px); transform: rotate(180deg); left: 100%;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="left: -30px; top: calc(50% - 15px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="transform: rotate(90deg); left: calc(50% - 15px); top: -60px;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> </div> </div> <div style="display: none;" class="accordion-content default"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> </div> </div> </div> <br> <div style="text-align:center">Lots of ocntent between</div> <br> <div class="container"> <div class="accordion"> <div class="accordion-toggle"> <div class="openclose"> <p class="resp-p man">I dont work :(</p> <img style="transform: rotate(-90deg); left: calc(50% - 15px); top: calc(100% + 30px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="top: calc(50% - 15px); transform: rotate(180deg); left: 100%;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="left: -30px; top: calc(50% - 15px);" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> <img style="transform: rotate(90deg); left: calc(50% - 15px); top: -60px;" src="http://simpleicon.com/wp-content/uploads/arrow-18.png"> </div> </div> <div style="display: none;" class="accordion-content default"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> </div> </div> </div> 

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

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