检查以下代码。我不清楚您上面所说的选择过程,但是在您选择文本后我有两个解决方案。希望能帮助到你。我用过 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);
});