2016-04-26 18 views
1

テキスト入力内にハイライトされたテキストがある場合、選択したテキストを削除するにはどうすればよいですか?入力テキストから選択したテキストを削除するには?

私はどれくらいの距離を持っているかを示すためにフィドルを設定しましたが、特定のテキストを削除する方法を理解できません。

<input type='text' value='stackoverflow.com' id='text1' /> 
 
<br /> 
 
<button id="btnSelect">select text</button> 
 
<button id="btnRemove">remove selected text</button>

+4

プレスバックスペースまたは削除 – guradio

+0

。値を空白に設定します。 – RJParikh

+0

これを参照してください:http://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript – Rayon

答えて

7

selectionStartselectionEndinputタグから選択したテキストを取得するためにあります。.. はデモ

$("#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>

関連する問題