Autor Zpráva
jonge
Profil
Začínám s JavaScriptem a chtěl bych si o něco vylepšit přidávání smajlíků do textarea a to tak, že smajlík se vloží na to místo, kde je kurzor. Pomůžete mi někdo? Děkuji

EDIT: Mimochodem, tady je můj současný skript:
function vlozSmajla(textarea, text) {

textarea = document.getElementById(textarea);
textarea.value += text;
textarea.focus();
}
Hooonza
Profil *
Jestli budeš používat BB-kód, můžeš využít toto (upravené s ohledem na podporu v různých prohlížečích):

--- Skript: ---

<script type="text/javascript">
<!--
function showsmile(happy) {
smiledone = false; G = document.formular.body;
if ((navigator.appName=='Microsoft Internet Explorer')&&(!window.opera)) {
if((document.selection)&&(G.value.indexOf(document.selection.createRa nge().text)>-1)) {
G.focus ();
if (!document.selection.createRange().text) {
document.selection.createRange().text = ' '+happy+' ';
smiledone = true; };};}
else { var selLength = G.textLength;
var selStart = G.selectionStart;
var selEnd = G.selectionEnd;
var s1 = (G.value).substring(0,selStart);
var s2 = (G.value).substring(selStart, selEnd);
var s3 = (G.value).substring(selEnd, selLength);
if (s2=='') G.value = s1 + ' '+happy+' ' + s3;
if (s2=='') smiledone=true;
};

if (!smiledone) G.value += ' '+happy+' '; G.focus();
};

//-->
</script>

-----------------------

V blízkosti textarey umístíš obrázky smajlíků s událostí onclick, na kliknutí se do textarey "name=body" formuláře "name=formular" vypíše zastupující znak:

<img src="smilie1.gif" border=0 onclick="showsmile('::x1'); ">

Zastupující znak potom pomocí PHP nahradíš za adresu obrázku.
Hooonza
Profil *
P.S.: Na 6. řádku skriptu má být "createRange" bez mezery. Funkční verze je tady.
jonge
Profil
Hooonza
Díky, ale už jsem si našel chybu v mém skriptu, chyběla mi tam špičatá závorka. Kdyby to někoho zajímalo, skript je tady:
function vlozSmajla(textarea, text) {

textarea = document.getElementById(textarea);

// pro IE a jiné prohlížeče, které podporují "document.selection"
if (document.selection) {
textarea.focus();
vyber = document.selection.createRange();
vyber.text = text;
}

// pro prohlížeče postavené na jádře Gecko
else if (textarea.selectionStart || textarea.selectionStart == 0) {
startPos = textarea.selectionStart;
endPos = textarea.selectionEnd;
textarea.value = textarea.value.substring(0, startPos) + text + textarea.value.substring(endPos, textarea.value.length);
}

else textarea.value += text;
}
Toto téma je uzamčeno. Odpověď nelze zaslat.

0