私はこのスクリプトを作成しましたが、それはうまくいきましたが、それはひどく繰り返され、DRYされなければならないことがわかりました。セレクタに変数を使用する方法を理解していますが、関数を繰り返さない方法はわかりません。誰も助けることができますか?これらの繰り返しjQueryセレクタと関数をどのようにDRYしますか?
// JS to add a "More" link to priority announcements when the height is too tall.
$(document).ready(function(){
// If JS is turned on add a height restriction to the announcement
$(".importantNotice .content").addClass("maxHeight");
// If the height is over 245 a "More" link apears
if ($('.importantNotice .content').height() >= 245 || $('.importantNotice .content .landing-no-display').length) {
$('.importantNotice .more').addClass("show");
}
// If the window gets resized it rechecks height
$(window).resize(function(){
if ($('.importantNotice .content').height() >= 245 || $('.importantNotice .content .landing-no-display').length) {
$('.importantNotice .more').addClass("show");
}
if ($('.importantNotice .content').height() < 245 ($.contains('.importantNotice .content').not(':has(.landing-no-display)')) {
$('.importantNotice .more').removeClass("show");
}
});
});
関数を定義しますが、 '.toggleClass()'に見たことがありますか? http://opi.jquery.com/toggleclass/ –
ここでどのようにtoggleClassのヘルプがありますか? @JoeB。 –
サイズ変更のためにあなたはできません。あなたが '.addClass()'と 'removeClass()'をする必要はないでしょうか?そうでなければ、あなたは '.add()'を呼び出すことができます。 –