2017-06-20 177 views
1

第一个问题,所以要种;)重构jQuery代码

我试图重构这一功能:

jQuery(document).ready(function($){ 

     //One 
     $('.flip').click(function(){ 
     var link = $(this); 
     $('.panel').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Two 
    $('.flip2').click(function(){ 
     var link = $(this); 
     $('.panel2').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Three 
    $('.flip3').click(function(){ 
     var link = $(this); 
     $('.panel3').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Four 
    $('.flip4').click(function(){ 
     var link = $(this); 
     $('.panel4').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Five 
    $('.flip5').click(function(){ 
     var link = $(this); 
     $('.panel5').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //six 
    $('.flip6').click(function(){ 
     var link = $(this); 
     $('.panel6').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Seven 
    $('.flip7').click(function(){ 
     var link = $(this); 
     $('.panel7').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Eight 
    $('.flip8').click(function(){ 
     var link = $(this); 
     $('.panel8').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 

    //Nine 
    $('.flip9').click(function(){ 
     var link = $(this); 
     $('.panel9').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 


    //Ten 
    $('.flip10').click(function(){ 
     var link = $(this); 
     $('.panel10').slideToggle('slow', function() { 
      if ($(this).is(":visible")) { 
       link.text('Close');     
      } else { 
       link.text('Read More');     
      }   
     });   
    }); 





    }); 

我希望它只是一个机能的研究可以做到这一点。我应该在哪里转弯?我应该阅读什么文章?我真的停留在这!

干杯,

编辑:

这是HTML:

<!-- section six --> 
<h3 class="text-center">How to use promotional feather banners?</h3> 
Feather banners are a brilliant way to guide people around your business premises, or liven up a local event with some custom advertising. These flags are very quick to set up — use them to transform a space in minutes. 
<div class="panel6"> 

These branded flags can be used in a cluster or on their own — depends on the effect you want to create. A single large flag can make a good impression on people passing by on foot or in their cars, whereas a cluster of smaller or medium flags can make a nice walkway through a retail forecourt, or show people where to walk at a large event. All our flags come in a range of sizes and sets to suit different budgets — just speak to one of the team who can take you through all the different options. 

Businesses that need to grab people’s attention fast often opt for flag advertising — real estate agents, food retailers, and car dealerships being just some of our regular flag customers. <strong>Our flags help Aussie businesses really stand out against their competition.</strong> 

Our flag bases and poles are all sold separately — we will help you find the right flag bundle for your needs. We stock both hard ground and soft ground feather flag kits so that you are always prepared for any event or eventuality. See for yourself how easy they are to set up and install. 

<center><button class="trigger-btn trigger-1250" style="padding: 10px;">Enquire today for some great offers</button></center> 
<div class="text-center"><strong>Flag us down and speak to an expert on 1300 556 589</strong></div> 
&nbsp; 

</div> 

<p class="flip6">Read More</p> 


<hr /> 

<!-- section six end --> 
+0

如何'.flipN'和'.panelN'有关?一个是另一个的父母吗?我还假设每一个都是一个单一的元素,在这种情况下,它们都应该是同一个类,因为它们表现出相同的行为。 –

+0

嗨帕特里克 - .flipN是一个链接,所以当用户点击.panelN将扩大或缩小:) –

+3

这可能会更好[codereview.stackexchange](https://codereview.stackexchange.com/) – empiric

回答

4

你可以做一个点击事件,如果你把所有的翻盖元素的同一类的,和通过某个属性将每个面板绑定到正确的翻盖。

例如,在给定的HTML:

<a class="flip" id="flip1 ">flip 1</a> 
<a class="flip" id="flip2">flip 2</a> 
<div class="panel panel1" data-id="flip1"></div> 
<div class="panel panel2" data-id="flip2"></div> 

它可能现在只能运行1个点击事件到这次行动

$('.flip').click(function(){ 
    var link = $(this); 
    var id = $(this).attr("id"); 
    // use the attribute data-id to find the element. 
    $('.panel[data-id='+ id +']').slideToggle('slow', function() { 
     if ($(this).is(":visible")) { 
      link.text('Close');     
     } else { 
      link.text('Read More');     
     }   
    });   
}); 
+0

'link'将是未定义的..你也需要检查'面板'是否可见或不是..目前'翻转' –

+0

是真的,我想回答他问的问题。如何使函数写入一次。 –

+0

我正要更新问题 –