0

我正在尝试将弹出窗口中的输入文本插入页面上的文本区域,从光标位置定义。

这个想法是,用户单击一个按钮,一个弹出窗口打开,用户可以在输入框中输入文本,然后单击一个按钮将该文本插入到光标位置所在的文本区域。

position = null;

function cursorPosition () {
 if(!window.getSelection)
 {
  position = document.selection.createRange().duplicate();
 }
 else 
 {
  position = window.getSelection();
 }
}

function insertAtCaret (text)
{
 position.text = text;
}

在弹出窗口中,我有:

function onclose(text)
{
 var newtext= text;
 opener.insertAtCaret(newtext);
 window.close();
}

不能让它在 chrome 中工作,只有 IE....每次我得到一个

未捕获的类型错误:对象 [object Window] 的属性“insertAtCaret”不是函数

任何想法让它在所有浏览器中工作?

4

2 回答 2

2

下面的代码接受来自输入框的输入,并根据需要将其添加到文本区域。它将输入框值附加到文本区域。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Try it</button>

<textarea id="demo" cols="30" rows="5">
</textarea>

<script>
function myFunction()
{
var x;

var name=prompt("Please enter your name","Harry Potter");

if (name!=null)
  {
  x = document.getElementById("demo").value; 
  x = x + " Hello " + name + "! How are you today?"; // textarea value is appended here.
  document.getElementById("demo").value=x;
  }
}
</script>

</body>
</html>
于 2013-02-18T08:32:11.210 回答
1

我认为 FF\Chrome 中的 insertAtCaret 存在问题,您可能需要检查一下

于 2013-02-18T08:21:04.307 回答