我有一個div內含一串內容。內容不存儲在變量中,我想刪除此div中文本的最後一個字符。請幫忙嗎?Javascript:如何從div或字符串中刪除最後一個字符?
更多信息:
我有一個文本字段,並在輸入文本到文本字段,文本顯示在大書寫一個div。當用戶點擊後退鍵時,我想要刪除div中的最後一個字符。我主要使用jQuery來實現這一點。這裏是我的代碼:
$(document).ready(function(){
$("#theInput").focus(); //theInput is the text field
$('#theInput').on('keypress', printKeyPress);
function printKeyPress(event)
{
//#mainn is the div to hold the text
$('#mainn').append(String.fromCharCode(event.keyCode));
if(event.keyCode ==8) //if backspace key, do below
{
$('#mainn').empty(); //my issue is located here...I think
}
}
});
我試圖$('#mainn').text.slice(0,-1);
我也曾嘗試存儲#theInput的價值在一個變量,然後打印出來,但沒有奏效。有任何想法嗎 ?
爲什麼不把輸入值放到div中? – jarvanJiang 2013-05-14 10:01:04
只是想學習一切。 :) – 2013-05-14 10:03:08
偉大的問題 - 我發現許多許多答案如何從字符串中修剪字符。你很好地表達了你的問題,以幫助我通過搜索找到它。謝謝! – Jason 2016-04-20 16:20:04