Jquery 隐藏摘录并在重新切换时重新显示
我已经尽了最大的努力去做这件事,。。花了我几个小时,我还是不能让它工作。 单击readmore时,它会隐藏摘录并显示完整内容,然后重新命名要隐藏的按钮,然后再次单击HIDE时,它必须再次显示摘录并隐藏完整内容 这是我的 jQuery代码Jquery 隐藏摘录并在重新切换时重新显示,jquery,Jquery,我已经尽了最大的努力去做这件事,。。花了我几个小时,我还是不能让它工作。 单击readmore时,它会隐藏摘录并显示完整内容,然后重新命名要隐藏的按钮,然后再次单击HIDE时,它必须再次显示摘录并隐藏完整内容 这是我的 jQuery代码 jQuery(document).ready(function () { jQuery('.testi-more').click(function (e) { e.preventDefault(); var same =
jQuery(document).ready(function () {
jQuery('.testi-more').click(function (e) {
e.preventDefault();
var same = jQuery(this).closest('.testi-wrapper');
jQuery(this).closest('.testi-wrapper').children('.testi-full').slideToggle('slow');
});
if (jQuery('.testi-full').is(": hidden")) {
jQuery(this).closest('.testi-wrapper').children('.testi-excerpt').show();
jQuery(this).closest('.testi-wrapper').children('.testi-more').html('Read moar');
} else {
jQuery(this).closest('.testi-wrapper').children('.testi-excerpt').hide();
jQuery(this).closest('.testi-wrapper').children('.testi-more').html('Hide');
}
});
请优化我的代码,然后解释为什么,这样我可以研究它。谢谢 这就是你要找的吗
jQuery(function($) {
$('.testi-more').on('click', function(e) {
e.preventDefault();
$(this).text(function(_,txt) {
return txt == 'Read more' ? 'Hide' : 'Read more';
}).closest('.testi-wrapper').children('.testi-full')
.slideToggle('slow').end().children('.testi-excerpt').slideToggle();
});
});
它切换滑动,还通过回调切换文本,在回调中检查当前文本,并根据当前文本的内容返回另一个文本。它必须隐藏摘录。是~!谢谢,,,但我认为@vinex08更优化了-至少更容易阅读,我仍然会使用html()的回调,比如->