Javascript 插入符号在DIV、Textbox、Textarea等内部的位置/选择
是否有一个从不同元素获取每个浏览器中插入符号位置和/或选择的总体解决方案。我正在寻找一个解决办法,我可以像巫婆一样执行 mGetCaretPosition(iControl)开关将返回其元素内的插入符号位置。Javascript 插入符号在DIV、Textbox、Textarea等内部的位置/选择,javascript,internet-explorer,firefox,selection,caret,Javascript,Internet Explorer,Firefox,Selection,Caret,是否有一个从不同元素获取每个浏览器中插入符号位置和/或选择的总体解决方案。我正在寻找一个解决办法,我可以像巫婆一样执行 mGetCaretPosition(iControl)开关将返回其元素内的插入符号位置。 我已经尝试了很多功能: 选择(窗口/文档)[document=IE,window=Opera] getSelection(窗口/文档)[document=Firefox,document=Chrome,document=Safari] 选择开始(输入/文本区域)[All] crater
我已经尝试了很多功能:
选择(窗口/文档)[document=IE,window=Opera]
getSelection(窗口/文档)[document=Firefox,document=Chrome,document=Safari]
选择开始(输入/文本区域)[All]
craterange(选择)
createTextRange(选择)
调用document.selection.createRange()等方法不会返回插入符号位置,因为它没有选择。设置tRange.moveStart('character',-X)时,X不是已知值。当你在div中使用这个符号,插入符号在中间时,它会在div之前使用代码。
< P>我今天已经建立了这个。这是youre response alex和其他谷歌搜索结果的组合。我已经在个人电脑上的IE9浏览器、Chrome浏览器、Opera浏览器、Safari浏览器和Firefox浏览器中进行了测试,也在HTC的Android浏览器上进行了测试,默认浏览器为Firefox浏览器、Chrome浏览器和Opera浏览器 只有移动设备上的Opera出现了一些问题 我的解决方案:
// Control
var BSControl = function(iControl)
{
// Variable
var tControl = (typeof iControl == 'string' ? document.getElementById(iControl) : iControl);
// Get Caret
this.mGetCaret = function()
{
// Resultaat aanmaken
var tResult = -1;
// SelectionStart
// *) Input & Textarea
if(tResult == -1 && (tControl.selectionStart || tControl.selectionStart == '0'))
{
tResult = tControl.selectionStart;
}
// ContentWindow.GetSelection
// *) IFrame
if(tResult == -1 && (tControl.contentWindow && tControl.contentWindow.getSelection))
{
var tRange= tControl.contentWindow.getSelection().getRangeAt(0);
tResult = tRange.startOffset;
}
// GetSelection
// *) Div
if(tResult == -1 && (window.getSelection))
{
var tRange= window.getSelection().getRangeAt(0);
tResult = tRange.startOffset;
}
// Resultaat teruggeven
return tResult;
}
// Set Caret
this.mSetCaret = function(iPosition)
{
// SelectionStart
// *) Input & Textarea
if(tControl.selectionStart || tControl.selectionStart == '0')
{
tControl.selectionStart = iPosition;
tControl.selectionEnd = iPosition;
return;
}
// ContentWindow.GetSelection
// *) IFrame
if(tControl.contentWindow && tControl.contentWindow.getSelection)
{
var tRange = tControl.contentDocument.createRange();
tRange.setStart(tControl.contentDocument.body.firstChild, iPosition);
tRange.setEnd(tControl.contentDocument.body.firstChild, iPosition);
var tSelection = tControl.contentWindow.getSelection();
tSelection.removeAllRanges();
tSelection.addRange(tRange);
return;
}
// GetSelection
// *) Div
if(window.getSelection)
{
var tSelection = window.getSelection();
var tRange= tSelection.getRangeAt(0);
tRange.setStart(tControl.firstChild, iPosition);
tRange.setEnd(tControl.firstChild, iPosition);
tSelection.removeAllRanges();
tSelection.addRange(tRange);
return;
}
}
// Get Selection
this.mGetSelection = function()
{
// Resultaat aanmaken
var tResult = null;
// SelectionStart
// *) Input & Textarea
if(tResult == null && (tControl.selectionStart || tControl.selectionStart == '0'))
{
tResult = this.mGet().substring(tControl.selectionStart, tControl.selectionEnd);
}
// ContentWindow.GetSelection
// *) IFrame
if(tResult == null && (tControl.contentWindow && tControl.contentWindow.getSelection))
{
var tSelection = tControl.contentWindow.getSelection()
tResult = tSelection.toString();
}
// GetSelection
// *) Div
if(tResult == null && (window.getSelection))
{
var tSelection = window.getSelection()
tResult = tSelection.toString();
}
// Resultaat teruggeven
return tResult;
}
// Set Selection
this.mSetSelection = function(iFrom, iUntil)
{
// SelectionStart
// *) Input & Textarea
if(tControl.selectionStart || tControl.selectionStart == '0')
{
tControl.selectionStart = iFrom;
tControl.selectionEnd = iUntil;
return;
}
// ContentWindow.GetSelection
// *) IFrame
if(tControl.contentWindow && tControl.contentWindow.getSelection)
{
var tRange = tControl.contentDocument.createRange();
tRange.setStart(tControl.contentDocument.body.firstChild, iFrom);
tRange.setEnd(tControl.contentDocument.body.firstChild, iUntil);
var tSelection = tControl.contentWindow.getSelection();
tSelection.removeAllRanges();
tSelection.addRange(tRange);
return;
}
// GetSelection
// *) Div
if(window.getSelection)
{
var tSelection = window.getSelection();
var tRange= tSelection.getRangeAt(0);
tRange.setStart(tControl.firstChild, iFrom);
tRange.setEnd(tControl.firstChild, iUntil);
tSelection.removeAllRanges();
tSelection.addRange(tRange);
return;
}
}
// Set
this.mSet = function(iValue)
{
// Afhankelijk van aanwezige property waarde instellen
if('value' in tControl)
{
tControl.value = iValue;
}else if('innerText' in tControl)
{
tControl.innerText = iValue;
}else if('textContent' in tControl)
{
tControl.textContent = iValue;
}else if('innerHTML' in tControl)
{
tControl.innerHTML = iValue;
}
}
// Get
this.mGet = function()
{
// Resultaat aanmaken
var tResult = null;
// Afhankelijk van aanwezige property waarde instellen
if('value' in tControl)
{
tResult = tControl.value;
}else if('innerText' in tControl)
{
tResult = tControl.innerText;
}else if('textContent' in tControl)
{
tResult = tControl.textContent;
}else if('innerHTML' in tControl)
{
tResult = tControl.innerHTML;
}
// Resultaat teruggeven
return tResult;
}
}
看看你想要什么样的插入符号位置?您想要
内容可编辑的元素和/或文本区域/文本输入的解决方案吗?插入符号位置应如何表示?