Disable drag and drop of selected text

后端 未结 2 1744
太阳男子
太阳男子 2021-01-25 03:44

When I use chrome, any selected text in an input or a text box can be dragged and dropped into another input/textarea. Is there any way to disable dragging of selected text?

相关标签:
2条回答
  • 2021-01-25 04:06

    document.getElementById("test").addEventListener("dragstart", function(e){
      e.preventDefault();
    });
    <input id="test" type="text" value="Drag text into textarea">
    <br>
    <textarea></textarea>

    0 讨论(0)
  • 2021-01-25 04:10

    Bind the cut, copy and paste events to the <input> box and prevent default actions.

    Update

    I also bound dragstart to #Textbox1 and now this also prevents dragging text from this input into another input. I verified this using Chrome.

    $(document).ready(function() {
      $('#TextBox1').on('copy paste cut dragstart',function(e) { 
        e.preventDefault(); //disable cut,copy,paste
        console.log('cut,copy & paste options are disabled !!');
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" id="TextBox1" placeholder="Can't cut this!" /><br />
    <input type="text" id="TextBox2" />

    0 讨论(0)
提交回复
热议问题