Autor | Zpráva | ||
---|---|---|---|
Dukolm Profil * |
potřeboval bych nějaký javascript v tlačítku který vloží před a za označený text html značky počáteční a koncovou
|
||
fajzen Profil |
#2 · Zasláno: 4. 8. 2009, 20:39:36
function obal(pred, za) { try { // IE var vyber = document.selection.createRange().text; document.selection.createRange().text = pred + vyber + za; } catch(e) { try { // Mozilla var txtarea= document.getElementById('textarea'); // predpokladám, že ide o textareu var selLength = txtarea.textLength; var selStart = txtarea.selectionStart; var selEnd = txtarea.selectionEnd; if (selEnd==1 || selEnd==2) selEnd=selLength; var s1 = (txtarea.value).substring(0, selStart); var s2 = (txtarea.value).substring(selStart, selEnd) var s3 = (txtarea.value).substring(selEnd, selLength); txtarea.value = s1 + pred + s2 + za + s3; } catch(e) { // nefunguje ani jeden spôsob... } } } |
||
Dukolm Profil * |
#3 · Zasláno: 4. 8. 2009, 21:52:48
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title></title> </head> <body> <script type="text/javascript" > <!-- function obal(pred, za) { try { // IE var vyber = document.selection.createRange().text; document.selection.createRange().text = pred + vyber + za; } catch(e) { try { // Mozilla var txtarea= document.getElementById('textarea'); // predpokladám, že ide o textareu var selLength = txtarea.textLength; var selStart = txtarea.selectionStart; var selEnd = txtarea.selectionEnd; if (selEnd==1 || selEnd==2) selEnd=selLength; var s1 = (txtarea.value).substring(0, selStart); var s2 = (txtarea.value).substring(selStart, selEnd); var s3 = (txtarea.value).substring(selEnd, selLength); txtarea.value = s1 + pred + s2 + za + s3; } catch(e) { // nefunguje ani jeden spôsob... } } } --> </script> <form> <input type="button" value="B" onClick="obal(b,a)"> <textarea rows="5" cols="40"></textarea> </form> </body> </html> nevím jestli jsem to do html vložil správně nějak mi to nefunguje |
||
sysel Profil |
#4 · Zasláno: 5. 8. 2009, 01:25:44
<form> <input type="button" value="B" onClick="obal('neco pred','neco za')"> <textarea rows="5" cols="40" id="textarea"></textarea> </form> |
||
Pan X Profil |
#5 · Zasláno: 5. 8. 2009, 06:53:53
Použij toto, jedná se upravený script ze zdejší diskuse
index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="cs"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="description" content="doplnit"> <meta name="keywords" content="doplnit"> <meta name="author" content="Ondřej Záruba"> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="js/js_insert.js"></script> <title>doplnit</title> </head> <body> <div id="tu"></div> <form name="frm" onsubmit="return false"> <a href="#" onClick="bbCode('b', true)">B</a> | <a href="#" onClick="bbCode('i', true)">I</a> | <a href="#" onClick="bbCode('u', true)">U</a> | <a href="#" onClick="bbCode('h1', true)">H1</a> | <a href="#" onClick="bbCode('h2', true)">H2</a> | <a href="#" onClick="bbCode('h3', true)">H3</a> | <a href="#" onClick="bbCode('h4', true)">H4</a> | <a href="#" onClick="bbCode('h5', true)">H5</a> | <a href="#" onClick="bbCode('h6', true)">H6</a> | <a href="#" onClick="bbCode('hr', false)">HR</a> | <a href="#" onClick="bbCode('url', true)">URL</a> | <a href="#" onClick="bbCode('img', true)">IMG</a> | <br> <textarea name="text" cols="100" rows="15" tabindex="2"></textarea> <button onclick="document.getElementById('tu').innerHTML = document.frm.text.value;">POšli</button> </form> </body> </html> js_insert.js /* ============================= * * === Definování proměnných === * * ============================= */ var startTag; var endTag; var bb; var inText; var predText; var zaText; var enter; /* ======================= * * === vložení BB kódu === * * ======================= */ function bbCode(tag, parovy){ var textarea = document.frm.text; if (typeof textarea.selectionStart != 'undefined') { //vse mimo IE var selection = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd); var stringStart = textarea.selectionStart; var stringEnd = textarea.selectionEnd; predText = textarea.value.substring(0,stringStart); var konec = document.frm.text.value.length; zaText = textarea.value.substring(stringEnd, konec); } else if (typeof document.selection != 'undefined') { //IE var selection = document.selection.createRange().text; } else { //alert('Bug!'); } if(parovy == true){ startTag = '<' + tag + '>'; endTag = '</' + tag + '>'; inText = selection; bb = predText + startTag + inText + endTag + zaText; }else if(parovy == false){ startTag = '<' + tag + '>'; endTag = ''; enter = '\n' textarea.focus(); } /* === Nastavení speciálních tagů === */ if(tag == 'url'){ if(inText == false){ inText = 'Odkaz'; }else{ inText = selection; } startTag = '<a href="http://www.example.com">'; endTag = '</a>'; } if(tag == 'img'){ if(inText == false){ inText = 'http://www.adresa-obrazku.cz/obrazek.jpg'; }else{ inText = selection; } startTag = '<img src="'; endTag = '">'; } bb = predText + startTag + inText + endTag + zaText; document.frm.text.value = bb; } |
||
Časová prodleva: 15 let
|
0