试试这个代码:上率先拿到选定文本textarea的
Some text with https://www.google.pl/?gws_rd=ssl for simple WYSIWYG function
点击,然后点击[URL来]按钮。
function make_url() {
var textComponent = document.getElementById("my_input");
var selectedText;
// IE version
if (document.selection != undefined)
{
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
// Mozilla version
else if (textComponent.selectionStart != undefined)
{
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos)
}
var link = document.getElementById("link");
var a = document.createElement("a");
var href = document.createTextNode("Link");
a.appendChild(href);
a.setAttribute("href", selectedText);
document.body.appendChild(a);
}
注意:每次你在textarea的添加新的文本,然后点击[URL来]按钮,一个新的超链接将generated.Also我已经使用jQuery库来选择,所以你必须将其附加在页面上。
希望它工作正常☻