我有下面的代码,它将值从当前光标位置的不同输入字段插入文本区域。 文本区域接收作为URL的输入。 textarea成功接收输入,但它以编码的形式出现。我希望它作为可点击的链接。可能吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Page</title>
<script type="text/javascript">
window.onload = function()
{
btn = document.getElementById("btnInsertText");
myText = document.getElementById("myTextArea");
title = document.getElementById("insTitle");
url = document.getElementById("insUrl");
btn.onclick = function()
{
insertAtCursor(myText, title.value, url.value);
}
}
function insertAtCursor(myField, title, url)
{
//IE support
if (document.selection)
{
myField.focus();
sel = document.selection.createRange();
sel.text = '<a href="'+url+'">'+title+'</a>';
}
//Mozilla/Firefox/Netscape 7+ support
else if (myField.selectionStart || myField.selectionStart == '0')
{
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+ '<a href="'+url+'">'+title+'</a>' + myField.value.substring(endPos, myField.value.length);
}
else
{
myField.value += myValue;
}
}
</script>
</head>
<body>
title: <input type="text" id="insTitle" /><br />
url: <input type="text" id="insUrl" />
<input type="button" id="btnInsertText" value="Insert Text" /><br /><br />
<textarea id="myTextArea" rows="6" cols="50">
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
</textarea>
</body>
</html>
答案 0 :(得分:0)
除非您通过CSS将链接放在textarea上,否则无法实现。