0

我想要一个html格式,我可以通过单击它来选择正文文本,它将出现在html同一页面的文本框中。例如,让我们说正文部分中的文本“示例” . 如果我点击它,它是文本“示例”将出现在同一页面的文本框中。提前谢谢...

<html>
<body>
    name <input type=text name=name>
    example[<- I WANT THIS TEXT TO BE APPEAR ON THE NAME TEXT BOX IF I CLICK ON IT]
</body>
</html>
4

2 回答 2

1

检查以下代码。我不清楚您上面所说的选择过程,但是在您选择文本后我有两个解决方案。希望能帮助到你。我用过 jQuery。

解决方案 1: 我提供了一个链接以在文本框中显示选择。

html:

<div> 
<p>THIS TEXT TO BE APPEAR ON THE NAME TEXT BOX IF I CLICK ON IT</p><br/>
<a href="#" id='click'> click</a><br/>
<input type='text' id='box1' value="Select Text" /> 
</div>

javascript/jQuery:

if(!window.Kolich){
  Kolich = {};
}

Kolich.Selector = {};
Kolich.Selector.getSelected = function(){
  var t = '';
  if(window.getSelection){
    t = window.getSelection();
  }else if(document.getSelection){
    t = document.getSelection();
  }else if(document.selection){
    t = document.selection.createRange().text;
  }
  return t;
}

Kolich.Selector.mouseup = function(){
  var st = Kolich.Selector.getSelected();
  if(st!=''){
    $('#box1').val(st);
  }
}

$(document).ready(function(){
 $('#click').click(Kolich.Selector.mouseup);
});

解决方案2:

HTML:

 <div> 
 <p>Thisdt I want to extract</p>
 <input type='text' id='box1' value="Select Text" /> 
 </div>

javascript/jQuery:

if(!window.Kolich){
  Kolich = {};
}

Kolich.Selector = {};
Kolich.Selector.getSelected = function(){
  var t = '';
  if(window.getSelection){
    t = window.getSelection();
  }else if(document.getSelection){
    t = document.getSelection();
  }else if(document.selection){
    t = document.selection.createRange().text;
  }
  return t;
}

Kolich.Selector.mouseup = function(){
  var st = Kolich.Selector.getSelected();
  if(st!=''){
    $('#box1').val(st);
  }
}

$(document).ready(function(){
  $(document).bind("mouseup", Kolich.Selector.mouseup);
});
于 2013-01-07T06:59:17.350 回答
0

试试下面的代码。双击任何单词后,它会显示在文本框中。

<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("body").dblclick(function () {
                $("#test").val(getSelectionText());
            });
        });

        function getSelectionText() {
            var text = "";
            if (window.getSelection) {
                text = window.getSelection().toString();
            } else if (document.selection && document.selection.type != "Control") {
                text = document.selection.createRange().text;
            }
            return text;
        }
    </script>
</head>
<body>
    <input id="test" type="text" name="name" value="" />
    <div>
        Lucky day</div>
    <p>
        Hello world!
    </p>
</body>
</html>
于 2013-01-07T07:08:18.517 回答