Scusate se continuo a postare codice, ma stavo pensando che se le funzioni devono rimanere quelle due che ho scritto io, allora è meglio unificarle in un'unica funzione per rimuovere le parti di codice in comune...
[CODE]
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Esempio per il Forum di HTML.it</title>
<script type="text/javascript">
function insertMetaChar(sStartTag, sEndTag) {
var oMsgInput = document.myForm.myTxtArea, nSelStart = oMsgInput.selectionStart, nSelEnd = oMsgInput.selectionEnd, sOldText = oMsgInput.value;
if (arguments.length > 1) {
oMsgInput.value = sOldText.substring(0, nSelStart) + sStartTag + sOldText.substring(nSelStart, nSelEnd) + sEndTag + sOldText.substring(nSelEnd);
oMsgInput.setSelectionRange(nSelStart + sStartTag.length, nSelEnd + sStartTag.length);
} else {
oMsgInput.value = sOldText.substring(0, nSelStart) + sStartTag + sOldText.substring(nSelEnd);
oMsgInput.setSelectionRange(nSelStart === nSelEnd ? nSelStart + sStartTag.length : nSelStart, nSelStart + sStartTag.length);
}
oMsgInput.focus();
}
</script>
<style type="text/css">
.intLink {
cursor: pointer;
text-decoration: underline;
color: #0000ff;
}
</style>
</head>
<body>
Quest'esempio funziona sia che ci sia del testo selezionato nella textarea sia che non ci sia.</p>
<form name="myForm">
[&nbsp;<span class="intLink" onclick="insertMetaChar('[B]','[\/B]');">grassetto</span> | <span class="intLink" onclick="insertMetaChar('[I]','[\/I]');">corsivo</span> | <span class="intLink" onclick="insertMetaChar('[URL='+prompt('Enter the full URL for the link')+']','[\/URL]');">collegamento</span> | <span class="intLink" onclick="insertMetaChar('\ncodice:\n','\n[\/CODE]\n');">codice</span> | <span class="intLink" onclick="insertMetaChar(' :-)');">smile</span> | etc. etc.&nbsp;]</p> <textarea rows="10" cols="50" name="myTxtArea">Testo di esempio</textarea></p> </form> </body> </html>![]()

Rispondi quotando