Is it possible to restrict the range of select all/Ctrl+A?

前端 未结 1 946
予麋鹿
予麋鹿 2020-12-19 10:39

I am working on a website where I would like to be able to display a box containing syntax-highlighted source code for the user to copy. When I click on the box, giving it f

相关标签:
1条回答
  • 2020-12-19 11:02

    You can use the document.createRange(); method to select the text from a particular element. and to handle the ctrl+a you can use the jQuery keydown method and can call the JS code to select the DIV text.

    var range = document.createRange();
    range.selectNode(document.getElementById(containerid));
    window.getSelection().addRange(range);
    

    please see jsfiddle here jsfiddle.

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