我有一个带字符串的文本框,想要检查用户是否在文本框字符串之间或文本框字符串末尾删除了文本。是否有任何javascript函数来执行此任务?我尝试了createTextRange()和setSelectionRange()但没有任何作用。
答案 0 :(得分:0)
<强> HTML 强>
<input type="text" id="myTextInput" value="test" />
<强> JS 强>
var myInput = document.getElementById('myTextInput');
var defaultValue = myInput.value;
myInput.addEventListener("input", function () {
var index = myInput.value.indexOf(defaultValue, 0);
if(index == 0)
alert('The text has been added at the end of the original string');
else if(index == -1)
alert('The text has been added inside the original string');
else
alert('The text has been added in the front of the original string');
}, false);