admin管理员组文章数量:1327674
Expected output: i want Read more & Read less click option be at the bottom with all content which include paragraph & list.. (Codepen shows Read more & Read less click option at the top) Here what i tried: HTML
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
<div class="content">Testing
<ul>
<li>first</li>
</ul>
</div>
Javascript:
$(document).ready(function () {
$(".content").hide();
$(".show_hide").on("click", function () {
var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
$(".show_hide").text(txt);
$(this).next('.content').slideToggle(200);
});
});
Codepen link for Read more & read less implementation: enter link description here
Expected output: i want Read more & Read less click option be at the bottom with all content which include paragraph & list.. (Codepen shows Read more & Read less click option at the top) Here what i tried: HTML
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
<div class="content">Testing
<ul>
<li>first</li>
</ul>
</div>
Javascript:
$(document).ready(function () {
$(".content").hide();
$(".show_hide").on("click", function () {
var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
$(".show_hide").text(txt);
$(this).next('.content').slideToggle(200);
});
});
Codepen link for Read more & read less implementation: enter link description here
Share Improve this question edited Jun 25, 2020 at 14:04 zab asked Jun 23, 2020 at 4:19 zabzab 952 gold badges2 silver badges7 bronze badges4 Answers
Reset to default 2To move Read More link to the bottom and have it work use "prev" instead of "next" method.
$(document).ready(function () {
$(".content").hide();
$(".show_hide").on("click", function () {
var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
$(".show_hide").text(txt);
$(this).prev('.content').slideToggle(200);
});
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">Testing
<ul>
<li>first</li>
</ul>
</div>
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
Try this :
$(document).ready(function () {
$(".show_hide").on("click", function () {
var txt = $(".content").hasClass('visible') ? 'Read More' : 'Read Less';
$(".show_hide").text(txt);
$(".content").toggleClass("visible");
});
});
.content {
height:90px;
overflow:hidden;
margin-bottom:10px;
}
.content.visible {
height:auto;
overflow:visible;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">Testing
<ul>
<li>first</li>
<li>first</li>
<li>first</li>
<li>first</li>
<li>first</li>
<li>first</li>
<li>first</li>
<li>first</li>
</ul>
</div>
<a href="#" class="show_hide" data-content="toggle-text">Read More</a>
You can use css for this
.show_hide{
position:absolute;
top:100%;
}
Or just put it at the bottom of a container that is what people do
Your content
class (which gets shown and hidden) contains all of your visible content. You just need to include some content that does not have the content
class before your a
element, something like:
$(document).ready(function() {
$(".content").hide();
$(".show_hide").on("click", function() {
var txt = $(".content").is(':visible') ? 'Read More' : 'Read Less';
$(".show_hide").text(txt);
$(this).next('.content').slideToggle(200);
});
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<h3>Always shown</h3>
<p>Some text</p>
</div>
<a href="#" class="show_hide">Read More</a>
<div class="content">
<h3>Sometimes shown</h3>
<p>More text</p>
</div>
本文标签: htmlRead more amp Read less in website page content using JavascriptStack Overflow
版权声明:本文标题:html - Read more & Read less in website page content using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742224569a2435953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论