栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

是否有Internet Explorer批准的selectionStart和selectionEnd替代品?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

是否有Internet Explorer批准的selectionStart和selectionEnd替代品?

我将再次发布此函数,以查看该问题是否与另一个问题相关联。

以下将在所有浏览器中完成工作,并处理所有新行问题,而不会严重影响性能。我经过一番折腾之后才来到这里,现在我很确信这是最好的功能。

更新

此函数确实假定textarea / input具有焦点,因此您可能需要在调用textarea的

focus()
方法之前先对其进行调用。

function getInputSelection(el) {    var start = 0, end = 0, normalizedValue, range,        textInputRange, len, endRange;    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {        start = el.selectionStart;        end = el.selectionEnd;    } else {        range = document.selection.createRange();        if (range && range.parentElement() == el) { len = el.value.length; normalizedValue = el.value.replace(/rn/g, "n"); // Create a working TextRange that lives only in the input textInputRange = el.createTextRange(); textInputRange.moveToBookmark(range.getBookmark()); // Check if the start and end of the selection are at the very end // of the input, since moveStart/moveEnd doesn't return what we want // in those cases endRange = el.createTextRange(); endRange.collapse(false); if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {     start = end = len; } else {     start = -textInputRange.moveStart("character", -len);     start += normalizedValue.slice(0, start).split("n").length - 1;     if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {         end = len;     } else {         end = -textInputRange.moveEnd("character", -len);         end += normalizedValue.slice(0, end).split("n").length - 1;     } }        }    }    return {        start: start,        end: end    };}var el = document.getElementById("your_input");el.focus();var sel = getInputSelection(el);alert(sel.start + ", " + sel.end);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/442293.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号