admin管理员组文章数量:1287513
I'm trying to implement something to limit the size of a <li>
. The function is that you can write an article with n characters but it should be displayed only 500character then I want to put a button like read more.
For the moment I have the button who es if the text is 500characters but I can't find how to limit the size...
Someone could give me advices ?
I'm trying to implement something to limit the size of a <li>
. The function is that you can write an article with n characters but it should be displayed only 500character then I want to put a button like read more.
For the moment I have the button who es if the text is 500characters but I can't find how to limit the size...
Someone could give me advices ?
Share Improve this question asked Dec 20, 2016 at 7:56 JeromeJerome 1,1922 gold badges17 silver badges40 bronze badges 4- 9 You'll need to post your code. – Panda Commented Dec 20, 2016 at 7:57
- @Panda I think it's useless...what could I show you ? The HTML code ? – Jerome Commented Dec 20, 2016 at 8:00
- are you familiar with javascripts substring function ? – madalinivascu Commented Dec 20, 2016 at 8:01
- @madalinivascu no I'm not but I open to learn a new things :) – Jerome Commented Dec 20, 2016 at 8:01
4 Answers
Reset to default 4You can always use substring to get only a subset of chars entered and redisplay that on mouseover
On MouseOver use substring to change text inside li tag.
var myLi = $('#your-li-id');
myLi.text(myLi.text().substring(0,500))
You could do it with jQuery. Look at this:
$(document).ready(function() {
var count = $("#count").text().length;
var max = 5 // Max chars inside the li
if (count > max) {
$("#count").text($("#count").text().slice( 0, max ));
$("#count").append(" read more..."); // Add here your button or whatever you like.
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li id="count">1234567890</li>
</ul>
Hope this will help.
Maybe you could style it with some CSS? Like with: (you have to define it's width either)
overflow: hidden;
text-overflow: ellipsis;
And than with jquery change class only on button click and then in CSS show full article?
You can try this:
<li id="demo">Some lenghty string goes here</li>
function myFunction() {
var str = "Hello world!";
var res = str.substring(1, 4);
document.getElementById("demo").innerHTML = res;
}myFunction();
https://jsfiddle/AwadheshVerma/7r4nac49/
if you want to on click:
<button onclick="myFunction()">Try it</button>
function myFunction() {
var str = "Hello world!";
var res = str.substring(1, 4);
document.getElementById("demo").innerHTML = res;
}
本文标签: javascriptHow to limit the size of an 39ltligt39 elementStack Overflow
版权声明:本文标题:javascript - How to limit the size of an '<li>' element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741312596a2371731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论