0

リファクタリングできないように見える次のコードがあります。

    //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 を必要とせず、クラスを使用するだけでなく、3 つの jquery ステートメントの必要性を減らすことができるように、コードをリファクタリングするにはどうすればよいですか。

4

1 に答える 1

1

、、およびDIVに追加class="accordion-header'します。それで:proofreadingediton_time

$(".accordion-header").mouseover(function() {
    $('.' + this.id + ' .accordion-body').collapse('show');
});
于 2013-08-27T08:19:29.757 に答える