基本思想是突出顯示輸入中指定長度值後的字符,並顯示通知消息。根據字符數突出顯示子文本
這裏,我們去:
<div id="test_box">
<input type="text" id="text_text">
</div>
CSS:
#notice {
width: 140px;
height: 40px;
background-color: black;
}
#test_box {
width: 400px;
height: 400px;
}
和jQuery代碼:
$(document).ready(function() {
var length = $('#text_text').val().length;
var char_limit = 5;
$('#text_text').bind('keyup', function() {
new_length = $('#text_text').val().length;
if (new_length > char_limit) {
$('#text_text').css('color','red');
$('#test_box').append('<div id="notice"> There are limit of character for home page title of this field </div>'); // wrong too much divs :/
} else {
$('#text_text').css('color', 'black');
$('#notice').hide(); //wrong
}
});
});
目前字符char_limit
後強調超標,我需要的是對只強調char_limit
後面的那些人。並且,如果我輸入字符,每次都會注意塊添加,我認爲我應該手動創建該div,或者可能不會在超出char_limit
時以某種方式顯示它。
你可以使用文本框的mexlength屬性嗎? ''這將迫使這個問題不需要腳本。 – jwatts1980
我不能這樣做,因爲我的網站用戶可以添加一些內容,每個內容都有標題,但在主頁上我需要顯示修剪標題和限制是5個atm,但這只是示例(限制將在稍後設置) ,所以我想讓用戶理解,如果他們在主頁上宣傳這個標題將會被修剪。 –
如果你不介意使用jQuery插件:[jQuery Caret Plugin](http://www.jquery-plugin.buss.hk/my-plugins/jquery-caret-plugin),可以通過指定開始在「文本輸入框」(輸入類型=「文本」)或「文本區域」(文本區域)中選擇的位置,結束位置或長度 – RASG