简体   繁体   English

非表单元素是否存在onselect事件?

[英]Is there a onselect event for non-form elements?

Say I want to hide a span when the user highlights a bit of text containing that span, with the intention of copying that text on to his clipboard. 假设我要在用户突出显示包含该范围的文本时隐藏一个范围,以将其复制到剪贴板上。

For example: 例如:

<p>The dragon <span class="tooltip">a large, mythical beast</span> belched
fire at St. George.</p>

I have found that in Firefox Mac, span.tooltip will disappear from view (in accordance with my CSS declarations) but will show up in the clipboard when it's copied there. 我发现在Firefox Mac中,span.tooltip会从视图中消失(根据我的CSS声明),但是将其复制到剪贴板时会显示在剪贴板中。 I figured (wrongly?) that if I said "onHighlight, hide tooltip," maybe this wouldn't happen. 我发现(错误?)如果我说“ onHighlight,隐藏工具提示”,也许不会发生。

Though it may be more complicated, why not just have an onmousedown event on the <p> element, and thet event will then attach an onmousemove event and onmouseout event, so that if there is a mouse movement, while the button is down, then remove the class on the span elements, and once the user exits, the element then you can put them back. 尽管可能更复杂,但为什么不只在<p>元素上发生onmousedown事件,然后thet事件将附加一个onmousemove事件和onmouseout事件,因此,如果在按钮按下时有鼠标移动,则删除span元素上的类,一旦用户退出,则可以将其放回原处。

It may be a bit tricky, and you may want to also look for key presses, or determine other times you want to know when to put back the css classes, but this would be one option, I believe. 这可能有点棘手,并且您可能还希望查找按键,或者确定其他时间想知道何时放回CSS类,但这是一个选择,我相信。

It sounds like you need to go one step further and on highlight, remove the <span> and save a reference to it. 听起来您需要更进一步,突出显示,删除<span>并保存对其的引用。 Once the highlight is finished, re-insert the reference to the object. 突出显示完成后,将引用重新插入到对象。

 // copy start detected
 var savedTooltip = $('.tooltip').remove();

 // later that day when copy finished 
 $('p').append(savedTooltip);

If position of the <span> in your markup is important, you'd have to create a temporary reference element so you'd know where to re-insert it in the DOM. 如果<span>在标记中的位置很重要,则必须创建一个临时引用元素,以便知道将其重新插入DOM的位置。

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

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