반응형
jQuery에서 문자열을 어떻게자를 수 있습니까?
나는 긴 제목을 가지고 있고 그것들을 자르고 싶지만 단어가 끊기지 않는 방식으로 단어를 자르지 않는 단어 사이에서 절단이 발생한다는 것을 의미합니다.
jquery를 사용하여 어떻게 할 수 있습니까?
From : jQuery 텍스트 잘림 (더 많은 스타일 읽기)
이 시도:
var title = "This is your title";
var shortText = jQuery.trim(title).substring(0, 10)
.split(" ").slice(0, -1).join(" ") + "...";
플러그인을 사용할 수도 있습니다.
String의 확장으로
String.prototype.trimToLength = function(m) {
return (this.length > m)
? jQuery.trim(this).substring(0, m).split(" ").slice(0, -1).join(" ") + "..."
: this;
};
로 사용
"This is your title".trimToLength(10);
위의 솔루션은 원래 문자열에 공백이 없으면 작동하지 않습니다.
이 시도:
var title = "This is your title";
var shortText = jQuery.trim(title).substring(0, 10)
.trim(this) + "...";
function truncateString(str, length) {
return str.length > length ? str.substring(0, length - 3) + '...' : str
}
jQuery를 사용하는 대신 css property를 사용하십시오 text-overflow:ellipsis. 자동으로 문자열을 자릅니다.
.truncated { display:inline-block;
max-width:100px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}
프로토 타입 및 공간 없음 :
String.prototype.trimToLength = function (trimLenght) {
return this.length > trimLenght ? this.substring(0, trimLenght - 3) + '...' : this
};
참고 URL : https://stackoverflow.com/questions/4637942/how-can-i-truncate-a-string-in-jquery
반응형
'program story' 카테고리의 다른 글
| Swift의 UINavigationBar 텍스트 색상 (0) | 2020.11.29 |
|---|---|
| 오류 1067 (42000) : 'created_at'에 대한 잘못된 기본값 (0) | 2020.11.29 |
| 다각형 교차를위한 간단한 알고리즘 (0) | 2020.11.28 |
| Windows 프로그램에서 WM_QUIT, WM_CLOSE 및 WM_DESTROY의 차이점은 무엇입니까? (0) | 2020.11.28 |
| PHP에서 HTML 양식 입력 기본값을 올바르게 이스케이프하는 방법은 무엇입니까? (0) | 2020.11.28 |