2013-08-27 27 views
0

我有下面的代码,我似乎不能重构:我如何重构这个jQuery代码为NOOB

//opens some drop downs 
    $('#proofreading').mouseover(function(){ 
       $('.proofreading .accordion-body').collapse('show'); 
    }); 


    $('#edit').mouseover(function(){ 
       $('.edit .accordion-body').collapse('show'); 
    }); 


    $('#on_time').mouseover(function(){ 
       $('.on_time .accordion-body').collapse('show'); 
    }); 

下面是在html代码:

<div class='content-more'> 
<br> 
<span id='proofreading'> 
Read More... 
</span> 
</div> 
<div class='accordion proofreading'> 
<div class='accordion-group'> 
<div class='accordion-body collapse'> 
<div class='accordion-inner'> 
<p class='content-desc'> 
<br> 
When your work is proofread at EAPI we will go over your text word by word, line by line correcting the spelling, punctuation, grammar, terminology, jargon, and semantics. As a result, your writing will be clear, correct, concise, comprehensible, and consistent: the <b>EAPI 5C standard</b>. 
</p> 
</div> 
<br> 
<span class='content-more close-accordion'> 
close 
</span> 
</div> 
</div> 
</div> 

<div class='content-more'> 
<br> 
<span id='edit'> 
read more... 
</span> 
</div> 
<div class='accordion edit'> 
<div class='accordion-group'> 
<div class='accordion-body collapse'> 
<div class='accordion-inner'> 
<p class='content-desc'> 
<br> 
When your work is edited at EAPI we will check and improve the formatting, style and accuracy of the text without altering the intended meaning. 
</p> 
<span class='content-more close-accordion'> 
close 
</span> 
</div> 
</div> 
</div> 

<div class='content-more'> 
<br> 
<span id='on_time'> 
Read more... 
</span> 
</div> 
<div class='accordion on_time'> 
<div class='accordion-group'> 
<div class='accordion-body collapse'> 
<div class='accordion-inner'> 
<p class='content-desc'> 
<br> 
When your work is proofread at EAPI we will go over your text word by word, line by line correcting the spelling, punctuation, grammar, terminology, jargon, and semantics. As a result, your writing will be clear, correct, concise, comprehensible, and consistent: the <b>EAPI 5C standard</b>. 
</p> 
</div> 
<br> 
<span class='content-more close-accordion'> 
close 
</span> 
</div> 
</div> 
</div> 

如何我可以重构代码,所以我不需要id,只需使用一个类,并减少对三个jquery语句的需求。

+0

我会建议通过更改HTML标记开始。使用Ids封装相关内容,您不必使用'on_time','edit'和'校对'来重新标识相关内容。 – Alvaro

回答

1

class="accordion-header'加上proofreading,editon_timeDIV。然后:

$(".accordion-header").mouseover(function() { 
    $('.' + this.id + ' .accordion-body').collapse('show'); 
}); 
+0

感谢Barmar的工作。 – chell