Javascript 除了活动的jQuery之外,所有div都返回默认值

Javascript 除了活动的jQuery之外,所有div都返回默认值,javascript,jquery,css,css-selectors,Javascript,Jquery,Css,Css Selectors,好的,这是我的剧本 当您单击“了解更多”按钮时,它对应于该特定框,并相应地下拉正确的信息 我想添加一个功能,这样当您单击另一个learn more时,已经有一个div打开并包含信息,它会自动关闭,就像此脚本中切换的结尾一样 基本上,我不想同时打开两个learn more。如果选择其中一个,则所有其余的都将关闭。按照页面的设置方式,我一次只能打开一个页面 $(document).ready(function(){ var service = $(".service"), text = $(

好的,这是我的剧本

当您单击“了解更多”按钮时,它对应于该特定框,并相应地下拉正确的信息

我想添加一个功能,这样当您单击另一个learn more时,已经有一个div打开并包含信息,它会自动关闭,就像此脚本中切换的结尾一样

基本上,我不想同时打开两个learn more。如果选择其中一个,则所有其余的都将关闭。按照页面的设置方式,我一次只能打开一个页面

$(document).ready(function(){ 


 var service = $(".service"), text = $(".service-text, h1.service-heading"), moretext = $(".more-text"); 


$(".learn").toggle(
               function()
               {
                $(this).parent().find(service).animate({ backgroundColor : "#000000" }, 800);
                $(this).animate({ backgroundColor : "#FFFFFF" }, 800);
                $(this).parent().find(text).animate({ color: "#FFF"}, 800);
                $(this).parent().find("span.more").animate({ color : "#000000" }, 800, function () {
                    $(this).parent().parent().find(".service").animate({ height : "500px"}, 800, function () {
                            $(this).find(moretext).show().animate({ color: "#FFFFFF"}, 800);

                            $(this).parent().find("span.more").animate({ color : "#FFF"}, 800, function () {
                                                                                                         $(this).hide();
                                                        $(this).parent().find("span.less").show( function () {
                                                                                                           $(this).animate({ color: "#000000"}, 800);
                                                                                                         });
                                                                                                         });
                                                                                                           });
                                                                                                 });
               },
               function()
               {
                $(this).parent().find(service).animate({ backgroundColor : "#FFFFFF" }, 800, function () {
                            $(moretext).animate({ color: "#FFFFFF"}, 800, function () {
                                                                                    $(this).hide();
                                                                                                       });
                             });
                $(this).animate({ backgroundColor : "#000000" }, 800);
                $(this).parent().find(text).animate({ color: "#000000"}, 800);
                $(this).parent().find("span.less").animate({ color: "#FFFFFF"}, 800, function() {
                    $(this).parent().parent().find(service).animate({ height: "180px"}, 800, function () {
                                        $(this).parent().find("span.less").animate({ color: "#000000"}, 800, function () {
                                                                                                                       $(this).hide();
                                                            $(this).parent().find("span.more").show( function () {
                                                                                                           $(this).animate({ color: "#FFFFFF"}, 800);
                                                                                                                       });
                                                             });
                                                                                                       });
                                                                                              });





                   });
   });
伪代码:

  • 给所有相关的div指定一个特殊的类名
  • 将toggle out末尾的“close”函数分离为一个单独的函数,使其不会使用“active”类关闭div
  • 设置一个侦听器,用于单击将类“active”添加到单击的对象的了解更多信息,然后在执行其操作之前为所有(其他)div调用关闭函数

    • 你可以这样做

      $(".learn").click(function(){
          $(".learn").not(this).doSomeThing(/* some code */);
      });
      

      通过此代码,您可以从集合中排除当前单击的项目

      您应该提供一个简单的代码!!假设上面的代码是有效的(我不熟悉jQuery),我假设
      /*一些代码*/
      可以隐藏/关闭除“this”之外的所有“learn more”?如果有更熟悉jQuery的人能澄清一下,我将不胜感激。:)由$(“.learn”)选择的集合。不(这)是除此之外的所有learn more按钮,doSomeThing(/*一些代码*/);是要对此集合执行的操作,因此可以(隐藏()、切换()、…)