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
Add a ment  | 

4 Answers 4

Reset to default 4

You 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