简体   繁体   English

文本区域接收输入值的URL,并在不带代码的文本区域中显示URL(

[英]text area recieve input value which URL and display the URL in text area without code (<a href=…)

I have below code, which insert value into a text area from a different input field on the current cursor position. 我有下面的代码,该代码将值从当前光标位置的不同输入字段插入文本区域。 The text area receives input which is a URL. 文本区域接收作为URL的输入。 The input is successfully received by the textarea but it appears in the form of coding. 输入已成功通过textarea接收,但以编码形式出现。 i want it as a link which clickable. 我想要它作为可点击的链接。 Is it possible? 可能吗?

<!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>

除非您通过CSS将链接放置在textarea上,否则这是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM