Pridanie tagu na začiatok a koniec riadku

Programovacie jazyky, rady, poradňa...
Peter Michalík
Light Expert
Light Expert
Príspevky: 44
Registrovaný: 11 máj 2008, 9:01

Pridanie tagu na začiatok a koniec riadku

Príspevok od používateľa Peter Michalík »

Potreboval by som doplniť do tohoto kódu jedno tlačítko, ktoré v riadku, kde je kurzor (teda bez označovania výberu textu kurzorom) by doplnil na začiatok riadku tag <h2> a na koniec </h2>.
A ešte tlačítko, ktoré by uložilo text v okienku do súboru output.html.

Help me please :)

Kód: Vybrať všetko

<script type="text/javascript">
      <!-- 
        function formatText(el,tagstart,tagend, tag) 
        {
  	  if (el.setSelectionRange) 
          {
 	    el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	  }
  	  else 
          {
  	    var selectedText = document.selection.createRange().text;
            if (selectedText != "") 
            {
              var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
              document.selection.createRange().text = newText;
            }
  	  }
        }
     //-->
</script>

  <form>
    <textarea id="mytext" rows="10" cols="50"></textarea><br>
    <input type="button" value="Bold" onclick="formatText(document.getElementById('mytext'),'<b>','</b>', 'b')"/>
    <input type="button" value="Italic" onclick="formatText(document.getElementById('mytext'),'<i>','</i>', 'i')"/>
    <input type="button" value="Underline" onclick="formatText(document.getElementById('mytext'),'<u>','</u>', 'u')"/>
    <input type="button" value="Link" onclick="formatText(document.getElementById('mytext'),'<a>','</a>', 'a')"/>
  </form>
Prílohy
FormatText.zip
(603 Bajtov) 14 stiahnutí
audiotrack
VIP
VIP
Používateľov profilový obrázok
Príspevky: 25958
Registrovaný: 09 sep 2005, 18:39
Kontaktovať používateľa:

Re: Pridanie tagu na začiatok a koniec riadku

Príspevok od používateľa audiotrack »

Kód: Vybrať všetko

<script type="text/javascript">
      <!-- 
        function formatText(el,tagstart,tagend, tag) 
        {
		
		if(tag == "h1"){
			el.selectionStart = el.value.substring(0,el.selectionEnd).lastIndexOf("\n")+1;
			el.selectionEnd = el.selectionStart + document.getElementById('mytext').value.substring(el.selectionStart,el.value.length).indexOf("\n");
			if(el.selectionStart == el.selectionEnd) {
				el.selectionStart += 1;
				el.selectionEnd = document.getElementById('mytext').value.length;
				}
		}
		
  	  if (el.setSelectionRange) 
          {
 	    el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	  }
  	  else 
          {
  	    var selectedText = document.selection.createRange().text;
            if (selectedText != "") 
            {
              var newText = "<" + tag + ">" + selectedText + "</" + tag + ">";
              document.selection.createRange().text = newText;
            }
  	  }
        }
		
		
		function getCursor(){
		
		var nBox = document.getElementById('mytext');
		var cursorPos = 0;
         	if (document.selection)
			{ 
	         	 nBox.focus();
             	 	 var tmpRange = document.selection.createRange();
  	         	 tmpRange.moveStart('character',-nBox.value.length);
        	 	 cursorPos = tmpRange.text.length;
     			}
        	else 	{
		 	 if (nBox.selectionStart || nBox.selectionStart == '0')
				{
				 cursorPos = nBox.selectionStart;
				}
			}
		var lineNumber =  parseInt(cursorPos/nBox.cols) + 1;  // 25 is the number of textarea columns
		alert(cursorPos);
		return lineNumber;
  	}


		
     //-->
</script>

  <form>
    <textarea id="mytext" rows="10" cols="50"></textarea><br>
    <input type="button" value="Bold" onclick="formatText(document.getElementById('mytext'),'<b>','</b>', 'b')"/>
    <input type="button" value="Italic" onclick="formatText(document.getElementById('mytext'),'<i>','</i>', 'i')"/>
    <input type="button" value="Underline" onclick="formatText(document.getElementById('mytext'),'<u>','</u>', 'u')"/>
    <input type="button" value="Link" onclick="formatText(document.getElementById('mytext'),'<a>','</a>', 'a')"/>
	<input type="button" value="header" onclick="formatText(document.getElementById('mytext'),'<h1>','</h1>', 'h1')"/>
  </form>
Peter Michalík
Light Expert
Light Expert
Príspevky: 44
Registrovaný: 11 máj 2008, 9:01

Re: Pridanie tagu na začiatok a koniec riadku

Príspevok od používateľa Peter Michalík »

Super, funguje to, ďakujem :)

Prosím ťa, vedel by si aj to save tlačítko?
audiotrack
VIP
VIP
Používateľov profilový obrázok
Príspevky: 25958
Registrovaný: 09 sep 2005, 18:39
Kontaktovať používateľa:

Re: Pridanie tagu na začiatok a koniec riadku

Príspevok od používateľa audiotrack »

ukladanie do súboru v php sa tu píše asi každý mesiac, mohol by si skúsiť pohľadať. Tak námatkovo: http://www.hojko.com/php-uprava-txt-sub ... hp%20fopen
Peter Michalík
Light Expert
Light Expert
Príspevky: 44
Registrovaný: 11 máj 2008, 9:01

Re: Pridanie tagu na začiatok a koniec riadku

Príspevok od používateľa Peter Michalík »

dík, pohľadám :)
Napísať odpoveď