要做一个功能就是在一个div中选中一段文字,然后把选中的加个背景。
function getSelectedText() {
if (window.getSelection) {
return window.getSelection().toString();
}else if (document.getSelection) {
return document.getSelection();
}else if (document.selection) {
return document.selection.createRange().text;
}
}
function Test(){
var t=getSelectedText();
if(t.length>0){
var content=document.getElementById('repFirstpage').innerHTML;
__global_notes__count++;
content=content.replace(t,"<span style='background-color:#ff0000'><strong>["+__global_notes__count+"]</strong>"+t+"</span>"); document.getElementById('repFirstpage').innerHTML=content;
$('#divNotes').show(100);
$("#txtNotes").select();
}
}
这个是我现在用的部分代码,加红色的部分会有bug,因为如果选中一个单词,如果这个单词有重复,那么就会把前面的那个替换成有背景的,因此我想获取选中的开始和结束为止,然后通过substring来实现。希望大侠给我一些方法
function getSelectedText() {
if (window.getSelection) {
return window.getSelection().toString();
}else if (document.getSelection) {
return document.getSelection();
}else if (document.selection) {
return document.selection.createRange().text;
}
}
function Test(){
var t=getSelectedText();
if(t.length>0){
var content=document.getElementById('repFirstpage').innerHTML;
__global_notes__count++;
content=content.replace(t,"<span style='background-color:#ff0000'><strong>["+__global_notes__count+"]</strong>"+t+"</span>"); document.getElementById('repFirstpage').innerHTML=content;
$('#divNotes').show(100);
$("#txtNotes").select();
}
}
这个是我现在用的部分代码,加红色的部分会有bug,因为如果选中一个单词,如果这个单词有重复,那么就会把前面的那个替换成有背景的,因此我想获取选中的开始和结束为止,然后通过substring来实现。希望大侠给我一些方法
转换为文本之后你就是找单词了。
<form>
<textarea id="hehe" style="width:500pt;height:500px">haha</textarea>
<input type=button value="select" onclick="alert(getSelected(document.getElementById('hehe')));">
</form>
<script type="text/javascript">
// 说明:获取页面上选中的文字 //
function getSelectedText() {
if (window.getSelection) { // This technique is the most likely to be standardized.
// getSelection() returns a Selection object, which we do not document.
return window.getSelection().toString();
}else if (document.getSelection) { // This is an older, simpler technique that returns a string
return document.getSelection();
}else if (document.selection) { // This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
return document.selection.createRange().text;
}
}
// 说明:FireFox 下获取 input 或者 textarea 中选中的文字
function getTextFieldSelection(e) {
var e=arguments[0];
if (e.selectionStart != undefined && e.selectionEnd != undefined) {
var start = e.selectionStart;
var end = e.selectionEnd;
return e.value.substring(start, end);
}else{
return ""; // Not supported on this browser
}
}
function getSelected(){
if(window.document.all){
if (window.getSelection) { // This technique is the most likely to be standardized.
// getSelection() returns a Selection object, which we do not document.
return window.getSelection().toString();
}else if (document.getSelection) {
// This is an older, simpler technique that returns a string
return document.getSelection();
}else if (document.selection) {
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
return document.selection.createRange().text;
}
}else{
var e=arguments[0];
if (e.selectionStart != undefined && e.selectionEnd != undefined) {
var start = e.selectionStart;
var end = e.selectionEnd;
return e.value.substring(start, end);
}else{
return ""; // Not supported on this browser
}
}
}
</script>
一段文字中如果有重复的话,你选中替换的时候会把先出现的文字替换掉,所以这个才是我要解决的。我要获取用户选中的字的开始位置,然后取数据
转换为文本之后你就是找单词了。
var cuRange = document.selection.createRange();
var tbRange = obj.createTextRange();
var headRange = document.selection.createRange();
tbRange.setEndPoint("EndToEnd", cuRange);
var poe = tbRange.text.length;
tbRange.setEndPoint("EndToStart", cuRange);
var pos = tbRange.text.length;
cuRange.select();
txt = value.substring(pos, poe);
其他浏览器可以通过
obj.selectionStart 和obj.selectionEnd OBJ代表input对象结合以上2者就可以知道input
标签里的选中的文本的开始位置和结束位置,半边之后来回答这个问题。嘿嘿