简体   繁体   English

如何将文本输入定向到html页面中的隐藏字段?

[英]How can I direct text inputs to a hidden field in a html page?

I'm trying to handle copying and pasting to an application I'm developing by redirecting them into a hidden field. 我正在尝试通过将它们重定向到隐藏字段来处理复制和粘贴到我正在开发的应用程序。 The problem is that, when I set visibility: hidden on the field I'm trying to direct the input to, typing or copy/pasting into the field doesn't seem to work. 问题是,当我设置visibility: hidden在字段上我试图将输入定向到,键入或复制/粘贴到字段中似乎不起作用。 I'm trying to use a <textarea> tag to catch the text, and setting the focus with document.select('textAreaID').focus() , and it works, unless I set visibility: hidden in the css. 我正在尝试使用<textarea>标记来捕获文本,并使用document.select('textAreaID').focus()设置document.select('textAreaID').focus() ,并且它可以工作,除非我设置visibility: hidden在css中。

Test fiddle that shows that behavior. 测试表明行为的小提琴。 Blue square gives focus, red and green toggle visibility. 蓝色方块提供焦点,红色和绿色切换可见性。

Is there a good way to hide the text area and still be able to paste or type into it? 是否有一种隐藏文本区域的好方法,仍然可以粘贴或输入文本区域? If not, is there a good alternate way to redirect copy behavior to yield a chunk of text that can be parsed, edited, and worked with? 如果没有,是否有一种很好的替代方法来重定向复制行为以产生可以解析,编辑和使用的一大块文本?

(Also: there's some d3 in the fiddle since I'm using it for something else and it offers convenient APIs.) (另外:小提琴中有一些d3,因为我将它用于其他东西,它提供了方便的API。)

It looks like browsers do not allow focusing on visibility:hidden elements. 看起来浏览器不允许关注可见性:隐藏元素。 It might be different on other browsers, but it wasn't working for me on Chrome: http://jsfiddle.net/ps6KK/ 在其他浏览器上可能会有所不同,但在Chrome上它并不适合我: http//jsfiddle.net/ps6KK/

So since we can't do visibility:hidden we need to essentially force the textarea to seem like it's hidden. 因为我们无法实现可见性:隐藏我们需要基本上强制textarea看起来像隐藏。 You can do that by wiping out all of it's height/weight/etc and setting the background to transparent. 您可以通过消除所有高度/重量/等并将背景设置为透明来实现。 You might also want to set the text color to transparent if you want. 如果需要,您可能还希望将文本颜色设置为透明。

#textDiv {
  resize: none;
  padding: 0;
  height: 0;
  border: 0;
  width: 0;
  background: transparent;
}

A more detailed example is here: http://jsfiddle.net/4Jwxv/1/ 这里有一个更详细的例子: http//jsfiddle.net/4Jwxv/1/

您可以使用jQuery中的.html()方法来更新textarea

Is it your problem storing the text when you "copy" (not "paste") it? 当你“复制”(而不是“粘贴”)时存储文本是你的问题吗? You can do that with a good old global JavaScript variable (not declared inside the event handler function, but outside); 您可以使用一个好的旧全局JavaScript变量(未在事件处理函数内部声明,但在外部); no need to store it into a hidden HTML element. 无需将其存储到隐藏的HTML元素中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM