[英]Insert hyperlink and remove hyperlink in Javascript
我想創建2個簡單的按鈕,如下所示:
用於在textarea中插入和刪除指向我的HTML的超鏈接。
我的代碼是:
function formatDoc(sCmd, sValue) {
oDoc = document.getElementById("textBox");
document.execCommand(sCmd, false, sValue); oDoc.focus();
}
在身體上:
<div><img class="intLink" title="Hyperlink" onclick="var sLnk=prompt('Inserire lURL','http://');if(sLnk&&sLnk!=''&&sLnk!='http://'){formatDoc('createlink',sLnk)}" src="link.gif" /></div>
<textarea id="textBox"><p>Lorem ipsum</p></textarea>
但這無法創建超鏈接並將其添加到我的元素。 怎么了?
ps:有沒有簡單的方法可以從元素中刪除鏈接?
您不能將超鏈接放在textarea或html中。 它被忽略。
您可以使用它轉換為超鏈接並在div中顯示
function ConvertToLinks() {
str = document.getElementById("S1").value;
str = str.replace(/\r\n|\n/g,'<br>');
document.getElementById('txtLinks').innerHTML = str;
}
<textarea rows="5" id="S1" name="S1" cols="40">
<a href="http://yahoo.com">Yahoo</a>
<a href="http://google.com">Google</a>
<a href="http://webdeveloper.com">Web Developer</a>
</textarea>
<br><button onclick="ConvertToLinks()">Convert to Links</button>
<div id="txtLinks" style="width:350px;min-height:100px;border:1px solid red"></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.