相关疑难解决方法(0)

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

找出在真实浏览器中选择的内容非常简单:

var range = {
  start: textbox.selectionStart,
  end: textbox.selectionEnd
}
Run Code Online (Sandbox Code Playgroud)

但IE像往常一样,不明白.什么是最好的跨浏览器方式?

javascript internet-explorer

19
推荐指数
3
解决办法
2万
查看次数

焦点更改时保持文本选择

我有一个普通的文本框和一个带文本输入的灯箱.

我希望将用户的选择保留在文本框中,即使用户专注于灯箱的文本输入也是如此.

  1. 在普通文本框中选择文本
  2. 切换灯箱
  3. 专注于灯箱输入

在步骤3,丢弃用户的文本选择.如何防止这种情况?例如,请参阅Google文档链接插入灯箱.

谢谢 :)

更新

好的,所以Google文档使用iframe作为空白页面部分,这就是他们处理多个选择的方式.像这样的东西(原谅令人作呕的HTML):

// test.html
<html><body>
  <h1 onclick='lightbox();'>This is the main section</h1>
  <iframe src='frame.html'></iframe>
  <div id='lightbox' style='display: none; position: fixed; top: 0; left: 0; height: 100%; width: 100%; opacity: 0.8; background-color: black;'>
    <input type='text' name='url' />
  </div>
  <script type='text/javascript'>
    function lightbox() {
      document.getElementById('lightbox').style.display = 'block';
    }
  </script>
</body></html>

// frame.html
<p>This is my iframe</p>
Run Code Online (Sandbox Code Playgroud)

iframe中的文本选择与灯箱中输入的焦点无关.因此,如果选择了某些文本"This is my iframe",则会切换灯箱并将光标放在输入中,iframe的文本选择会在没有任何javascript的情况下持续存在.

我现在正在尝试Nickolay的建议.

javascript textbox focus range selection

4
推荐指数
1
解决办法
1万
查看次数