admin管理员组文章数量:1220960
I have a js function which toggles between two spans of texts. One is smaller text with class collapsed and the other is full text with class expanded.
here is my js
$(document).ready(function() {
$(".expanded").hide();
$(".expanded, .collapsed").click(function() {
$(this).parent().children(".expanded, .collapsed").toggle();
});
});
and my html look like this
<span class="collapsed">
some small text
<a href="javascript:void(0)"> READ MORE>></a>
</span>
<span class="expanded">
here come the full text to replace the small text
<a href="javascript:void(0)">Less>></a>
</span>
The preset function replaces the text of one part with another. My problem is that when we click on any part of any span either collapsed or expanded, the text is changed. I want to restrict it to change the text only when one click the READ MORE>> or Less>> link. I have gone so far in this design that I cant change the function now. I want changes while staying the in present function. Any ideas??
I have a js function which toggles between two spans of texts. One is smaller text with class collapsed and the other is full text with class expanded.
here is my js
$(document).ready(function() {
$(".expanded").hide();
$(".expanded, .collapsed").click(function() {
$(this).parent().children(".expanded, .collapsed").toggle();
});
});
and my html look like this
<span class="collapsed">
some small text
<a href="javascript:void(0)"> READ MORE>></a>
</span>
<span class="expanded">
here come the full text to replace the small text
<a href="javascript:void(0)">Less>></a>
</span>
The preset function replaces the text of one part with another. My problem is that when we click on any part of any span either collapsed or expanded, the text is changed. I want to restrict it to change the text only when one click the READ MORE>> or Less>> link. I have gone so far in this design that I cant change the function now. I want changes while staying the in present function. Any ideas??
Share Improve this question edited Dec 25, 2014 at 10:51 Muhammad Kamran asked Dec 25, 2014 at 10:18 Muhammad KamranMuhammad Kamran 5611 gold badge7 silver badges19 bronze badges 3- When it's expanded, where is the full text added? in the span after the a tag ? – Billy Commented Dec 25, 2014 at 10:35
- The full text is added in the expanded span before the link. – Muhammad Kamran Commented Dec 25, 2014 at 10:41
- right now the function is expanding the text by clicking any where in the collapsed span either on link or on text. this is the problem i want to restrict it on the link only. – Muhammad Kamran Commented Dec 25, 2014 at 10:42
4 Answers
Reset to default 6You can use Readmore.js
$('#read').readmore({
speed: 75,
maxHeight: 100
});
EXAMPLE
$(document).ready(function() {
$(".expanded").hide();
$(".expanded a, .collapsed a").click(function(eve) {
eve.preventDefault();
$(".expanded, .collapsed").toggle();
});
});
fiddle
UPDATE
To solve the problem related to the posts.
$(document).ready(function() {
$(".expanded").hide();
$(".expanded a, .collapsed a").click(function(eve) {
var $container = $(this).parents("div");
eve.preventDefault();
$container.children(".expanded, .collapsed").toggle();
});
});
fiddle
Note that this code takes for granted that the spans are contained in a div (change if otherwise).
Hope that helps or at least gives you some ideas.
Kind regards.
How about this ? this will work on classes and is expandable.
$(".expanded a, .collapsed a").click(function() {
$(this).parent().children(".expanded, .collapsed").toggle();
});
Use this jQuery snippet to dig this issue
jQuery(document).ready(function() {
jQuery(".expanded").hide();
jQuery(".collapsed a").click(function() {
jQuery(this).hide();
jQuery(".expanded").slideDown();
});
jQuery(".expanded a").click(function(){
jQuery(".expanded").slideUp();
jQuery(".collapsed a").show();
});
});
本文标签: javascripthow to expand the text on clicking the read more button or linkStack Overflow
版权声明:本文标题:javascript - how to expand the text on clicking the read more button or link? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739320264a2157999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论