Jump to content


Photo

Inserir Algo No Texto Selecionado Quando Clicar Em Botão


  • Faça o login para participar
1 reply to this topic

#1 GreyWolf

GreyWolf

    Mestre

  • Usuários
  • 674 posts
  • Sexo:Não informado

Posted 17/08/2006, 14:06

Boa Tarde raça.

Estou fazendo um sistema de administração e nele da pra inserir negrito, itálico, links e aquele monte de coisas que se faz como aqui no fórum.

Eu usava um script que ele pegava o texto selecionado de qualquer parte e quando eu clicasse no estilo, ele mudava pro selecionado, problema é que só funciona no Internet Explorer, a função secundária pra mozilla não funciona.

No caso seria este:

function mozWrap(lft, rgt) {
	alert(document.content.selection.textLength);
	var selLength = document.content.texto.textLength;
	var selStart = document.content.texto.selectionStart;
	var selEnd = document.formulario.content.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (document.content.texto.value).substring(0,selStart);
	var s2 = (document.content.texto.value).substring(selStart, selEnd)
	var s3 = (document.content.texto.value).substring(selEnd, selLength);
	document.content.texto.value = s1 + lft + s2 + rgt + s3;
}

function IEWrap(lft, rgt) {
	strSelection = document.selection.createRange().text;
	if (strSelection!="") {
		document.selection.createRange().text = lft + strSelection + rgt;
	} else {
		document.formulario.texto.focus()
		strSelection = document.selection.createRange().text;
		document.formulario.texto.value = document.formulario.texto.value + lft + rgt;
	}
}

function wrapSelection(lft, rgt) {
	//if (document.all) {IEWrap(lft, rgt);}
	//else if (document.getElementById) {mozWrap(lft, rgt);}
	mozWrap(lft, rgt);
}
function wrapSelectionWithLink() {
	var my_link = prompt("Enter URL:","http://");
	if (my_link != null) {
		lft="<a href=\"" + my_link + "\">";
		rgt="</a>";
		wrapSelection(lft, rgt);
	}
	return;
}

O uso seria assim: <a href="#self" onclick="wrapSelection('[N]','[/N]'); this.blur();">[N]</a>


O amigo Klaus me indicou um outro site que continha um script genérico, porém eu descobri que só funciona pra pegar o texto selecionado se usar onmouseup, caso use ele clicando no link, no FireFox não funciona.

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}
	else return;
	//document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
	
	document.getElementById( "descricao" ).value = txt;
}

Uso: <a href="java script:;" onclick="getSel();">[N]</a>

Sem contar que neste ai tem que definir daonde que caixa ele irá pegar a seleção, já no primeiro, ele pega de qualquer lugar =]

Gostaria que me dessem opniões de como posso arrumar isto, mechi, revirei mas não consegui arrumar =]

Obrigado

#2 carneiro

carneiro

    programador web

  • Usuários
  • 155 posts
  • Sexo:Não informado
  • Localidade:cascavel - pr
  • Interesses:programação e design para web

Posted 12/03/2007, 10:45

Consegui resolver ele aqui..

1º Criei uma funcao para guardar o id do campo que obeteve o ultimo focus
function setIdCampo(id)

2º No campo eu inclui o onblur para esta funcao, entao qdo eu chamar a funcao para incluir o estilo
ele vai incluir no ultimo id que foi enviado no setIdCampo.

<input name="est_dsc" size="96" [b]onblur="setIdCampo(this.id)"[/b] id="est_dsc" value="" type="text"><br>
<input name="nome" size="96" [b]onblur="setIdCampo(this.id)"[/b] id="nome" value="" type="text"><br>


var IDCAMPO;
function setIdCampo(id){
	IDCAMPO = id;
}

function fstilos(tagIni,tagEnd){
	
	//Internet Explorer
	if(document.selection) {
	// use document.selection
	   var selectedText = document.selection.createRange().text;
	   if (selectedText != "") {
		   var newText = tagIni + selectedText + tagEnd;
		   document.selection.createRange().text = newText;
	   } 	
	
	// Firefox 
	}else if(window.getSelection){
		
	alert(IDCAMPO);
	var selLength = $(IDCAMPO).textLength;
	var selStart = $(IDCAMPO).selectionStart;
	var selEnd = $(IDCAMPO).selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = ($(IDCAMPO).value).substring(0,selStart);
	var s2 = ($(IDCAMPO).value).substring(selStart, selEnd)
	var s3 = ($(IDCAMPO).value).substring(selEnd, selLength);
	  $(IDCAMPO).value = s1 + tagIni + s2 + tagEnd + s3;		

	}
}

Aqui ta funcionando Legal..
Qualquer coisa soh postar ai :)
Descrição
Alexandre Magno Semmer
www.alexandresemmer.com
Programador Web

Programação
ASP, PHP, JavaScript, CSS, SQL, JAVA, C, Paschal, Ajax, XML
Usuário
Posted Image
Links
Ajax Online - FireBug (Firefox) - Documentação PHP - Documentação Prototype - Documentação jQuery - Script.Aculo.Us




0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq