JS Modify BACKSPACE Event

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.