用Javascript检测掉入textarea [英] Detecting drop into textarea with Javascript

查看:88
本文介绍了用Javascript检测掉入textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的就是能够检测何时将文本拖放到Textarea中。然后,我取该文本并对其进行处理并清除文本区域。这些文本区域可能很多,而且对UI的期望很高,所以轮询是不得已的方法。



对于IE而言, onfocus可以解决问题,因为触发了此事件用户将内容放到文本区域后。



对于Firefox,我找不到有效的事件。我已经尝试过onmouseup和onchange了。我不愿意轮询文本区域以获取内容。帮助非常感谢。谢谢。



编辑:为澄清起见,放置是指用户(通常)从页面中选择文本,但无论在何处,拖动并放置它进入文本区域。这与Ctrl + V或右键单击粘贴(采用两种不同的检测方法BTW)或(显然)在文本区域中键入不同。具体来说,它是拖放的放置方面。我真的不知道该怎么说。



我觉得这个问题讲得很准确。为了使我幽默,请假设我是指您都选择共享的文本区域上的任何其他操作。如果我的意思是粘贴,您是否认为我会提到有关问题标题或内容的某事?在文本区域中的键入也是如此。但是,也许,你们所有人都不是很了解我,只是知道我键入了我的意思,而不是错误地键入了仅与我的意思有关的东西。

解决方案

轮询似乎是唯一的方法。



在浏览器中启动拖放操作后,似乎取消发生的所有其他事情。您可以通过在页面上的某些内容上放置 onmouseover事件,然后开始DnD操作并将其拖到它上来见证这一点。该事件将不会触发。 IE和Safari。我怀疑W3规范中是否定义了很多内容...没关系,W3很烂,而且没有人完全遵守它。



在我的问题中,我说每次使用后都将文本区域清空的奢侈行为,所以我觉得轮询:textarea.value.length> 0几乎不花任何费用,并且不应该成为性能问题。



但是,可惜的是,它与将内容放入文本区域特别相关。



降级以减轻痛苦。


All I need is to be able to detect when text is dropped into a Textarea. I then take that text and do stuff with it and clear the textarea. There may be many of these textareas and very high UI expectations, so polling is a last resort.

For IE, "onfocus" does the trick, since this event is fired after the user drops stuff into the textarea.

For Firefox, I can't find an event that works. I've tried onmouseup and onchange.. don't know what else to try from there. I'd hate to have to poll the textarea for content. Help much appreciated. Thanks.

Edit: For clarification, "dropped" means the user selects text (usually) from the page, but it doesn't matter where, drags it, and drops it into the textarea. This is not the same as Ctrl+V, or right click pasting (which take two different methods of detection, BTW), or (obviously) typing into the textarea. Specifically, it is the "drop" aspect of drag and drop. I really don't know how else to phrase it.

I feel this question was stated rather accurately. To humor me, assume that I meant any of the other operations on a textarea that you all have chosen to share. If I meant "pasting", don't you think I would have mentioned something about pasting in the title or content of my question? Same goes for typing in a textarea. But, perhaps, you all just don't know me well enough to know that I type what I mean, rather than mistakingly typing things only somewhat related to what I mean.

解决方案

Polling seems to be the only way to go.

When a Drag+Drop operation is started within the browser, it seems to cancel out everything else that's going on. You can witness this by putting "onmouseover" events on something on the page, then starting a DnD operation and dragging over it. The events will not fire.

It seems only a coincidence of how the browser internally handles "drag and drop" that it causes onfocus() to be fired for the textarea in IE and Safari. I doubt there is much defined in the W3 specification... not that it matters, W3 is crap, and besides nobody adheres to it completely.

In my question I stated the luxury of emptying out the textarea after each use, so I feel that polling for: textarea.value.length>0 is hardly any cost and shouldn't be much of a performance concern.

But, alas, it specifically has to do with dropping content into a textarea.

Down-mod away to sooth your pain.

这篇关于用Javascript检测掉入textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆