简体   繁体   English

选择并拖动div或textarea的内容

[英]Selecting + dragging the contents of a div or textarea

I want to select the contents of a DIV (like highlight when you click and drag) when the user clicks on a button. 我想在用户单击按钮时选择DIV的内容(例如单击并拖动时突出显示)。 The (hidden) div contains text that needs to be dragged to a 3rd party app outside the browser as input. (隐藏的)div包含需要作为输入输入到浏览器外部的第三方应用程序的文本。 In other words: I want to simulate the user selecting the contents of a textarea, click and dragging it to another app. 换句话说:我想模拟用户选择文本区域的内容,单击并将其拖动到另一个应用程序。

I'm using the PrototypeJS framework, and I came this far: 我正在使用PrototypeJS框架,而我走到了这一步:

  • put an mousedown observer on the button that needs to be pressed. 将mousedown观察器放在需要按下的按钮上。

I've searched pretty hard already to find a way to select text in an html element, but couldn't find anything. 我已经进行了相当艰苦的搜索,以找到一种在html元素中选择文本的方法,但是找不到任何东西。 The Prototype API doesn't seem to have a good function that can select the contents of a textarea or an element. Prototype API似乎没有可以选择文本区域或元素内容的良好功能。 It does have Form.Element.select() but that only works on input fields apparently. 它确实具有Form.Element.select(),但显然仅适用于输入字段。

Can someone help me out? 有人可以帮我吗?

Based on the answer I gave yesterday to a similar question, in Prototype you could do it this way: 根据我昨天给一个类似问题的答案 ,在Prototype中,您可以这样进行:

$('elId').observer('click', function(e){
  var selectTarget = Event.element(e); // Get the element
  if(selectTarget != null) {
     if(selectTarget.tagName == 'TEXTAREA' || (selectTarget.tagName == "INPUT" && selectTarget.type == "text")) {
         selectTarget.select();
     } else if(window.getSelection) { // FF, Safari, Opera
         var sel = window.getSelection();
         var range = document.createRange();
         range.selectNode(selectTarget);
         sel.removeAllRanges();
         sel.addRange(range);
     } else { // IE
         document.selection.empty();
         var range = document.body.createTextRange();
         range.moveToElementText(selectTarget);
         range.select();
     };
  };
});

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

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