如何为NOOB重构此jquery代码

时间:2013-08-27 08:12:00

标签: jquery refactoring

我有以下代码似乎无法重构:

    //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语句的需求。

1 个答案:

答案 0 :(得分:1)

class="accordion-header'添加到proofreadingediton_time DIV。然后:

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