如何从输入文本中删除所选文本?

War*_*ula 1 html javascript php jquery

如果文本输入中有突出显示的文本,如何删除所选文本?

我设置了一个小提琴,以显示我有多远,但我无法弄清楚如何删除给定的文本.

<input type='text' value='stackoverflow.com' id='text1' />
<br />
<button id="btnSelect">select text</button>
<button id="btnRemove">remove selected text</button>
Run Code Online (Sandbox Code Playgroud)

Dha*_*ara 7

selectionStartselectionEnd在那里获得从选定的文本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;
});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)