Deselect contenteditable element

后端 未结 3 406
臣服心动
臣服心动 2021-01-28 15:05

I am trying to remove selection inside a contenteditable field. The code I have is this:

Text

3条回答
  •  清酒与你
    2021-01-28 15:45

    I found this on SO, and should be what you are looking for:

    if (window.getSelection) {
        if (window.getSelection().empty) {  // Chrome
            window.getSelection().empty();
        } else if (window.getSelection().removeAllRanges) {  // Firefox
            window.getSelection().removeAllRanges();
        }
    } else if (document.selection) {  // IE?
        document.selection.empty();
    }
    

    Working fiddle

提交回复
热议问题