gpt4 book ai didi

javascript - 使用 JavaScript 将链接 URL 和标题文本从表单输入字段插入光标位置的文本区域

转载 作者:行者123 更新时间:2023-11-30 23:45:08 24 4
gpt4 key购买 nike

我在 stackoverflow 上发现了一些代码,它将文本从文本输入字段插入到光标位置的文本区域中。

我想要做的是修改此工作代码,以便用户可以在两个表单输入字段中输入带有标题文本的 URL,从输入构建完整的超文本链接,并将 anchor 标记的生成 HTML 插入文本区域,如下所示光标位置处的完整链接,就像在 wysiwig 编辑器中单击插入 URL 按钮时发生的情况一样。

我如何修改下面的代码来实现这一点?

<!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");
text = document.getElementById("textToInsert");
btn.onclick = function()
{
insertAtCursor(myText, text.value);
}
}

function insertAtCursor(myField, myValue)
{
//IE support
if (document.selection)
{
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}

//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)+ myValue
+ myField.value.substring(endPos, myField.value.length);
}

else
{
myField.value += myValue;
}
}
</script>

</head>
<body>
Text To Insert: <input type="text" id="textToInsert" />

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

最佳答案

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

However not tested in IE!

关于javascript - 使用 JavaScript 将链接 URL 和标题文本从表单输入字段插入光标位置的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3259140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com