1

我正在尝试制作一个非常简单的富文本编辑器,它将所选文本包装在 bbcode 标记中。

Javascript:

<script type="text/javascript">
function wrapText(elementID, openTag, closeTag) {
    var textArea = document.getElementById(elementID);

    if (typeof(textArea.selectionStart) != 'undefined') {
        var begin = textArea.value.substr(0, textArea.selectionStart);
        var selection = textArea.value.substr(textArea.selectionStart, textArea.selectionEnd - textArea.selectionStart);
        var end = textArea.value.substr(textArea.selectionEnd);
        textArea.value = begin + openTag + selection + closeTag + end;
    }
}
</script>

HTML:

<form action="" method="POST">
<button type="button" onclick="wrapText('edit','[b]','[/b]');" >B</button>
<button type="button" onclick="wrapText('edit','[i]','[/i]');" >I</button>
<button type="button" onclick="wrapText('edit','[u]','[/u]');" >U</button><br />
<textarea id="edit" name="message" rows="10" cols="50">
This is some example text.
</textarea><br />
<br /><input type="submit" class="submit" value="Submit" />
</form>

这似乎在大多数情况下都可以正常工作,除了在 IE 中它仅在版本 10 中有效。

JSFiddle

我可以添加或更改什么以使这个更跨浏览器兼容,以便它可以与 IE 8 一起使用?

我不反对使用 jQuery,但我真的不想使用,除非没有它真的这么复杂

4

0 回答 0