クリックしたときにjQueryを使用してタグのテキストを変更しようとしています。スタックオーバーフローに関する他の質問から、これは正しいはずですが、なんらかの理由でうまくいきません。jQueryがタグ内のテキストを変更しない
テキストは、divブロックの表示/非表示をトリガーする「続きを読む」行です。
表示/非表示部分は機能しますが、「詳細を読む」のテキストは適宜「閉じる」に切り替える必要があります。助言がありますか?
これはヘッダにロードされたJavaScript、(jqueryのもこの時点でロードされている)である。
</script>
$(document).ready(function() {
$('.faq_question').click(function() {
if ($(this).parent().is('.open')){
$(this).closest('.faq').find('.faq_answer_container').animate({'height':'0'},500);
$(this).closest('.faq').removeClass('open');
$(this).closest('.faq_toggle h4').text('Read More');
}else{
var newHeight =$(this).closest('.faq').find('.faq_answer').height() +'px';
$(this).closest('.faq').find('.faq_answer_container').animate({'height':newHeight},500);
$(this).closest('.faq').addClass('open');
$(this).closest('.faq_toggle h4').text('Close');
}
});
});
</script>
これはCSSです:
.faq_question {
margin: 0px;
padding: 0px 0px 5px 0px;
display: inline-block;
cursor: pointer;
font-weight: bold;
}
.faq_answer_container {
height: 0px;
overflow: hidden;
padding: 0px;
}
.faq_toggle {
color: #00adef;
}
そして最後に、マークアップ:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
<div class="std">
<div class="faq_container">
<div class="faq">
<div class="faq_answer_container">
<div class="faq_answer">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
</div>
<div class="faq_question"><h4 class="faq_toggle"><strong>Read more</strong></h4></div>
</div>
</div>
</div>
前述のとおり、表示/非表示は機能しますが、[続きを読む]テキストは[閉じる]に切り替わりません。
は、私はあなたの問題の作業例が含まれるように編集されていますが、編集を元に戻しているように見えます。 – showdev
@showdev - はい、しました。私は作業サンプルへの変更に感謝しますが、文法やその他のテキストも変更しましたが、これは不要です(1つのケースでは間違っていました)。それを混乱させないでください。 – Tim
私は同意しません。もちろん、自分の投稿を適切に編集する権利があります。 – showdev