處理IE9下onpropertychange無法觸發最簡潔的方法
阿新 • • 發佈:2019-02-17
在處理當使用者輸入多少字同時顯示剩餘輸入字數時,在ie11下實現,然測試提出在ie9下原本實現的方法無法實現,故去查詢方法,發現onpropertychange在ie9下無法觸發,故將觸發條件增加即可,
1.增加onpaste當用戶貼上時觸發,
2.onfocus當用戶取消焦點時觸發,
3.onkeyup當用戶鍵盤按鍵鬆開時觸發,
程式碼如下:
<textarea name="content" id="suggestionContent"
**onpaste="return onCharsChange(this);"
onpropertychange="return onCharsChange(this);"
onfocus="return onCharsChange(this);"
onkeyup="return onCharsChange(this);"**
cols="45" rows="5"
class="textarea easyui-validatebox"></textarea>
js程式碼如下:
var cap_max = 100;//可傳送的字數
function onCharsChange(varField) {
var suggestionContent = document.getElementById("suggestionContent");
var charsmonitor1 = document.getElementById("charsmonitor1");
var charsmonitor2 = document.getElementById("charsmonitor2" );
var leftChars = getLeftChars(varField);
if (leftChars >= 0) {
//charsmonitor1.value=cap_max-leftChars;
//charsmonitor2.value=leftChars;
charsmonitor1.innerHTML = cap_max - leftChars;
charsmonitor2.innerHTML = leftChars;
return true;
} else {
charsmonitor1.value = cap_max;
charsmonitor2.value = "0";
//window.alerts("訊息內容超過字數限制。");
var len = suggestionContent.value.length + leftChars;
suggestionContent.value = suggestionContent.value.substring(0, len);
leftChars = getLeftChars(suggestionContent);
if (leftChars >= 0) {
charsmonitor1.innerHTML = cap_max - leftChars;
charsmonitor2.innerHTML = leftChars;
}
return false;
}
}
function getLeftChars(varField) {
var cap = cap_max;
var leftchars = cap - varField.value.length;
return (leftchars);
}