25 September 2016

Giới hạn độ dài của text trên Website với Javascript Html? How to limit the length of text in a paragraph?

Input:
<p>Breaking India: Western Interventions in Dravidian and Dalit 
Faultlines is a book written by Rajiv Malhotra and Aravindan 
Neelakandan which argues that India's integrity is being undermined.</p>
//Javascript:

function truncateText(selector, maxLength) {
    var element = document.querySelector(selector),
        truncated = element.innerText;

    if (truncated.length > maxLength) {
        truncated = truncated.substr(0,maxLength) + '...';
    }
    return truncated;
}

document.querySelector('p').innerText = truncateText('p', 107);

Output: 
Breaking India: Western Interventions in Dravidian and Dalit Faultlines is a book written by Rajiv Malhotra...

//Input
<p>Breaking India: Western Interventions in Dravidian and Dalit Faultlines 
is a book written by Rajiv Malhotra and Aravindan Neelakandan which argues 
that India's integrity is being undermined.</p>
//Style:
p {
  width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
//Output:Breaking India: Western Interventio...

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang