如何镜像文本选择?
假设您有两个输入:
和
< /code>
这样,通过一些 JavaScript 魔法,您在 input1 中键入的任何内容也会被放入 input2 中。从这个意义上说,input2“镜像”input1。
有没有一种方法可以“镜像”文本选择。因此,如果我在 input1 中选择一些文本,如何才能在 input2 中选择完全相同的文本?
我一直在研究 Microsoft 的 TextRange 对象和 Mozilla 等人使用的 Selection 对象,但整个混乱看起来相当麻烦。以前有人成功做过类似的事情吗?
澄清:感谢您到目前为止的回复。需要明确的是:我不是问如何镜像文本。我已经解决了这个问题。问题只是选择 input1 中的文本并选择 input2 中的相应文本。
Say you have two inputs:
<input id="input1" type="text"/>
and <input id="input2" type="text"/>
such that through some JavaScript magic, anything you type in input1 will also be put in input2. In this sense input2 "mirrors" input1.
Is there a way that I can also "mirror" text selection. So if I select some text in input1, how can I have the exact same text in input2 also be selected?
I've been looking at Microsoft's TextRange object and the Selection object used by Mozilla et al, but the whole mess seems pretty cumbersome. Has anyone had any success doing something like this before?
CLARIFICATION: Thanks for the responses so far. To be clear: I'm not asking how to mirror the text. I've already solved that one. The question is only about selecting the text in input1 and having the corresponding text in input2 also be selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,这在 Firefox 和 Opera 中非常简单。 Google Chrome 和 IE 则不然。这是在 FF 中运行的代码:
It's very easy in Firefox and Opera as far as I see. Google Chrome and IE not so much. Here's the code that works in FF:
您可以执行您询问的第一部分(就像我的回答此处),但不是第二部分 - 只是一次不可能有多个活动选定范围。
You can do the first part you ask about (like my answer here), but not the 2nd part - it's just not possible to have more than one active selected range at a time.
如果您可以接受使用 JQuery,那么可以通过以下方式完成克隆:
If you can live with using JQuery, then cloning can be done this way: