0

文字列を含むテキストボックスがあり、ユーザーがテキストボックス文字列の間またはテキストボックス文字列の最後にテキストをドロップしたことを確認したいと考えています。このタスクを実行する Javascript 関数はありますか? 試しcreateTextRange()てみsetSelectionRange()ましたが、何も機能しません。

4

1 に答える 1

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);

デモ </p>

于 2012-11-02T08:23:14.240 に答える