Web 接口
HTMLTextAreaElement - 表示 <textarea> 元素
在 textarea 中插入一些 HTML 标签,字符表情或任何自定义文本。
源代码:
点击运行 »
<style> /* 用于装饰内部 span 的 CSS 以表现为链接: */ .intLink { cursor: pointer; text-decoration: underline; color: #0000ff; } </style> <script> function insertMetachars(sStartTag, sEndTag) { var bDouble = arguments.length > 1, oMsgInput = document.myForm.myTxtArea, nSelStart = oMsgInput.selectionStart, nSelEnd = oMsgInput.selectionEnd, sOldText = oMsgInput.value; oMsgInput.value = sOldText.substring(0, nSelStart) + (bDouble ? sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag : sStartTag) + sOldText.substring(nSelEnd); oMsgInput.setSelectionRange(bDouble || nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, (bDouble ? nSelEnd : nSelStart) + sStartTag.length); oMsgInput.focus(); } </script> <form name="myForm"> <p>[ <span class="intLink" onclick="insertMetachars('<strong>','<\/strong>');"><strong>加粗</strong></span> | <span class="intLink" onclick="insertMetachars('<em>','<\/em>');"><em>斜体</em></span> | <span class="intLink" onclick="var newURL=prompt('输入链接的完整 URL');if(newURL){insertMetachars('<a href=\u0022'+newURL+'\u0022>','<\/a>');}else{document.myForm.myTxtArea.focus();}">URL</span> | <span class="intLink" onclick="insertMetachars('\n<code>\n','\n<\/code>\n');">代码</span> | <span class="intLink" onclick="insertMetachars(' :-)');">微笑表情</span> | 等等。 ]</p> <p><textarea name="myTxtArea" rows="10" cols="50">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut facilisis, arcu vitae adipiscing placerat, nisl lectus accumsan nisi, vitae iaculis sem neque vel lectus. Praesent tristique commodo lorem quis fringilla. Sed ac tellus eros. Sed consectetur eleifend felis vitae luctus. Praesent sagittis, est eget bibendum tincidunt, ligula diam tincidunt augue, a fermentum odio velit eget mi. Phasellus mattis, elit id fringilla semper, orci magna cursus ligula, non venenatis lacus augue sit amet dui. Pellentesque lacinia odio id nisi pulvinar commodo tempus at odio. Ut consectetur eros porttitor nunc mollis ultrices. Aenean porttitor, purus sollicitudin viverra auctor, neque erat blandit sapien, sit amet tincidunt massa mi ac nibh. Proin nibh sem, bibendum ut placerat nec, cursus et lacus. Phasellus vel augue turpis. Nunc eu mauris eu leo blandit mollis interdum eget lorem. </textarea></p> </form>
运行结果:
点击运行 »