用鼠标选择各种输入的文本

时间:2016-12-09 10:26:41

标签: javascript mouseevent

我希望通过鼠标选择它们来获取输入值(类型文本),并通过单击鼠标移动来复制它们的值

我有这样的意见:

<input type="text" value="one" />
<input type="text" value="two" />
<input type="text" value="three" />
<input type="text" value="four" />

我想复制(在我的剪贴板中)我的输入文本1&amp; 2&amp; 3将鼠标移到它们上面

不只是一个输入......而是只通过一次鼠标移动而输入多个输入,然后我将执行Ctrl-C

1 个答案:

答案 0 :(得分:1)

Please try the following code, this might solve your problem

FIDDLE

$('input[type=text]').mouseover(function() {
  var abtest = '';

  $('input[type=text]').each(function() {
    abtest = abtest + ', ' + $(this).val();
  });

  if (abtest.indexOf(',') >= 0)
    abtest = abtest.substring(1, abtest.len);

  $('#lbltest').text(abtest);
  SelectText('lbltest');
});



function SelectText(element) {
  var doc = document,
    text = doc.getElementById(element),
    range, selection;
  if (doc.body.createTextRange) {
    range = document.body.createTextRange();
    range.moveToElementText(text);
    range.select();
  } else if (window.getSelection) {
    selection = window.getSelection();
    range = document.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" value="one" />
<input type="text" value="two" />
<br>
<input type="text" value="three" />
<input type="text" value="four" />
<br>
<br>
<br>
<label id="lbltest">abtest</label>