selectionStart
和selectionEnd
正在那裏得到來自input
標籤所選文本.. 檢查演示
$("#btnSelect").click(function() {
document.getElementById('txtbox').setSelectionRange(6, 12);
});
$("#btnRemove").click(function() {
var ele = document.getElementById('txtbox');
var text = ele.value;
text = text.slice(0, ele.selectionStart) + text.slice(ele.selectionEnd);
ele.value = text;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' value='stackoverflow.com' id='txtbox' />
<br />
<button id="btnSelect">select text</button>
<button id="btnRemove">remove selected text</button>
按退格或刪除 – guradio
。設定值爲空白。 – RJParikh
請參閱:http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript – Rayon