Na gut... :)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<title>Tags in Textarea schreiben</title>
<script type="text/javascript">
// <![CDATA[
function cur_ins(field, startTag, endTag, offset) {
field.focus();
if ((window.getSelection && document.createRange) || document.getSelection) { // FF, Op., Ko., Safari
var scrollTop, scrollLeft;
if (field.type == 'textarea' && typeof field.scrollTop != 'undefined') {
scrollTop = field.scrollTop;
scrollLeft = field.scrollLeft;
}
selStart = field.selectionStart;
selEnd = field.selectionEnd;
text = field.value.substring(selStart, selEnd);
field.value = field.value.substring(0, selStart) + startTag + text + endTag + field.value.substring(selEnd);
if (typeof scrollTop != 'undefined') {
field.scrollTop = scrollTop;
field.scrollLeft = scrollLeft;
}
if (text.length > 0) {
if (offset != 0) {
field.selectionEnd = selStart + startTag.length - offset;
} else {
field.selectionEnd = selStart + startTag.length + text.length + endTag.length;
}
} else {
field.selectionEnd = selStart + startTag.length;
}
field.selectionStart = field.selectionEnd;
} else if (document.selection && document.selection.createRange) { // IE
marker = document.selection.createRange();
text = marker.text;
marker.text = startTag + text + endTag;
marker = document.selection.createRange();
if (text.length > 0) {
if (offset != 0) {
marker.move('character', startTag.length - offset);
} else {
marker.move('character', startTag.length + text.length + endTag.length + offset);
}
} else {
marker.move('character', -(endTag.length));
}
marker.select();
}
}
// ]]>
</script>
</head>
<body>
<div>
<form method="post">
<fieldset>
<legend>Nachricht</legend>
<textarea name="message" id="message" cols="40" rows="8"></textarea>
</fieldset>
<fieldset>
<button onclick="cur_ins(this.form.message, '<strong>', '</strong>', 0); return false;"><strong>B</strong></button>
<button onclick="cur_ins(this.form.message, '<em>', '</em>', 0); return false;"><em>I</em></button>
</fieldset>
</form>
</div>
</body>
</html>
Das ist im Übrigen die Funktion für das
Einfügen der BB-Code Tags hier im Forum, an der Jörg und ich geschraubt haben. Die funktioniert browserübergreifend, soweit wir es bisher testen konnten.