开发者

how to determine if the browser supports selectionStart if there is no text selected

开发者 https://www.devze.com 2023-01-04 18:09 出处:网络
This code will return true if the browser supports selectionStart and some text is selected, but if no text is selected it returns false (even on brows开发者_如何学JAVAers that support it):

This code will return true if the browser supports selectionStart and some text is selected, but if no text is selected it returns false (even on brows开发者_如何学JAVAers that support it):

   if (el.selectionStart) {

  }

How do you determine if the property is available regardless of whether text happens to be selected?

Thanks


Further googling revealed the answer:

 if (el.selectionStart != undefined) {

 }


Hopefully this will help you. I tested it on an old Android 4.2 (which returns false) and Chrome (which returns true).

function selectionSupport() {
    var input = document.createElement("input");
    input.setAttribute('value', '111');
    input.selectionStart = 1;
    input.selectionEnd = 2;
    return (input.selectionStart === 1 && input.selectionEnd === 2);
}

var selectionIsSupported = selectionSupport();
0

精彩评论

暂无评论...
验证码 换一张
取 消