When I was working on a project recently, there was a need to rewrite the keyboard's backspace event.
The first is an editable div,div with tags (li,li as empty tags) and text.
Demand questions:
1. When you delete text, you cannot delete the previous empty label.
2, the label can be deleted directly.
You need to override the keyboard's BACKSPACE event here. The code is as follows:
$ (document). On ("KeyDown", "#txtEditerBox",function(e) {if(E.keycode = = 8){ varSelection =window.getselection (); varRange = selection.getrangeat (0); //when the cursor is set to a point, the contents of the range are empty, and you need to reassign the range document that you chose to delete the last text of the previous node . if(range.collapsed) {if(range.startoffset===0){ //Range.setstart (range.startContainer.nextElementSibling); //Range.setstartoffset (range.startContainer.length);Range.Start =range.startContainer.previousElementSibling; varid = range.start? Range.start.id: ""; $ (". C-textarea"). Find ("#" +id). Remove (); //range.startoffset = range.startContainer.length;}Else{ Range.setstartoffset (range.startoffset-1); range.startoffset = range.startoffset-1; } } Console.log (range); //Delete the document area represented by the current Range object. range.deletecontents (); E.preventdefault (); e.stoppropagation (); }});
JS Modify BACKSPACE Event