반응형
jQuery를 사용하여 문자열에서 마지막 문자를 삭제하는 방법?
문자열에서 마지막 문자를 삭제하는 방법(예:123-4-삭제할 때4표시되어야 합니다.123-jQuery를 사용합니다.
평이한 자바스크립트로 시도해 볼 수도 있습니다.
"1234".slice(0,-1)
음의 두 번째 매개 변수는 마지막 문자에서 오프셋이므로 -2를 사용하여 마지막 2개 문자 등을 제거할 수 있습니다.
왜 jQuery를 사용합니까?
str = "123-4";
alert(str.substring(0,str.length - 1));
물론입니다.
jQuery가 포함된 서브스트레이트:
//example test element
$(document.createElement('div'))
.addClass('test')
.text('123-4')
.appendTo('body');
//using substring with the jQuery function html
alert($('.test').html().substring(0,$('.test').html().length - 1));
@skajfes와 @GollezTrol이 사용하기에 가장 좋은 방법을 제공했습니다.저는 개인적으로 'slice()'를 더 좋아합니다.코드도 적고, 줄이 얼마나 긴지 알 필요가 없습니다.사용만 하면 됩니다.
//-----------------------------------------
// @param begin Required. The index where
// to begin the extraction.
// 1st character is at index 0
//
// @param end Optional. Where to end the
// extraction. If omitted,
// slice() selects all
// characters from the begin
// position to the end of
// the string.
var str = '123-4';
alert(str.slice(0, -1));
일반 자바스크립트로 할 수 있습니다.
alert('123-4-'.substr(0, 4)); // outputs "123-"
문자열의 처음 4자를 반환합니다(조정).4고객님의 요구에 맞게)
이 페이지는 Google에서 "마지막 문자 제거 jquery"를 검색할 때 먼저 나타납니다.
이전의 모든 답이 맞지만, 왠지 내가 원하는 것을 빠르고 쉽게 찾을 수 있도록 도와주지 못했습니다.
뭔가 부족한 느낌이 듭니다.복제하는 경우 사과합니다.
jQuery
$('selector').each(function(){
var text = $(this).html();
text = text.substring(0, text.length-1);
$(this).html(text);
});
아니면
$('selector').each(function(){
var text = $(this).html();
text = text.slice(0,-1);
$(this).html(text);
})
언급URL : https://stackoverflow.com/questions/4308934/how-to-delete-last-character-from-a-string-using-jquery
반응형
'programing' 카테고리의 다른 글
| Http에 매개변수를 추가하는 방법NameValuePair를 사용하여 POST를 사용한 URL 연결 (0) | 2023.10.31 |
|---|---|
| WordPress 3.5 이후에도 Media_upload_tabs 필터를 사용하여 미디어 관리자에 탭을 추가해야 합니까? (0) | 2023.10.31 |
| CSS 이전 또는 이후의 SVG 채우기 색상 변경 (0) | 2023.10.26 |
| ggplot2를 사용할 때 지속적으로 잘못된 그래픽 상태 오류가 발생함 (0) | 2023.10.26 |
| 포인터 + 1이 4를 추가하는 이유 (0) | 2023.10.26 |