Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

brunoo

Janela de aviso

Recommended Posts

Olá pessoal http://forum.imasters.com.br/public/style_emoticons/default/thumbsup.gif Eu tenho um pequeno código que faz a palavra ficar em negrito e Itálico. Quando aperto no link negrito abre abas <b> quando aperto novamente fecha abas </b>. Eu queria saber como posso fazer para quando o usuário abrir abas sem fechar apareça uma mensagem dizendo que tem abas abertas.

 

O meu código:

 

function Selection(idTextArea){	this.idTextArea = idTextArea	this.selectionLength = 0;	this.selectionStart = 0;	this.selectionEnd = 0;	this.getSelectionText = getSelText;	this.setSelectionText = setSelText;	this.txtObj = document.getElementById(idTextArea);			if(document.selection)	{				this.txtObj.focus();		var range = document.selection.createRange().duplicate();		this.selectionLength = range.text.length;		var i = this.txtObj.value.length + 1;				range.collapse(true);		while(range.parentElement() == this.txtObj && range.move("character",1)==1 )		{			i--;		}				if(i)		{			this.selectionStart = i;			this.selectionEnd = this.selectionStart + this.selectionLength;		}						}	else 	{		this.selectionLength = this.txtObj.selectionEnd - this.txtObj.selectionStart;		this.selectionStart = this.txtObj.selectionStart;		this.selectionEnd = this.txtObj.selectionEnd;	}}function setSelText(strText){	if(document.selection) 	{		this.txtObj.focus();		var range = document.selection.createRange();		range.text = strText;		range.move("character", range.text.length);		this.txtObj.focus();	}	else	{		var arrtext = new Array();		arrtext[arrtext.length] = this.txtObj.value.substr(0, txt.selectionStart);		arrtext[arrtext.length] = strText;		arrtext[arrtext.length] = this.txtObj.value.substr(this.txtObj.selectionEnd);		var rangeStart = this.txtObj.selectionStart + strText.length;		var rangeEnd = rangeStart;		this.txtObj.value = arrtext.join('');		this.txtObj.focus();		this.txtObj.setSelectionRange(rangeStart, rangeEnd);	}}function getSelText(){	if (document.selection)	{		this.txtObj.focus();		var range = document.selection.createRange();		return range.text 	}	else	{		var text = this.txtObj.value.substr(this.txtObj.selectionStart, this.selectionLength);		return text;	}}function getLink(){	var url = prompt("Insira a URL no campo abaixo", "www.");	var text = prompt("Insira o título do link", '');	var arrData = new Array();	arrData[arrData.length] = (url == null) ? '' : url;	arrData[arrData.length] = (text == null) ? '' : text;	return arrData;}var arrTags = new Array();function insertCode(textarea, code, hasCloseTag){	var bbcode = "<" + code + ">";	var bbcodeclose = "</" + code + ">";		txt = document.getElementById(textarea);	var selection = new Selection(textarea);			if(selection.selectionLength == 0)	{		if(hasCloseTag && code != 'a href')		{			re = new RegExp(".*\\<(\\/*)" + code + "\\>.*$");			var text = txt.value.substr(0, selection.selectionStart);			if( re.test(text) )			{				match = re.exec(text);				if(match[1] == "/")				{					selection.setSelectionText(bbcode);									if(!arrTags[""+textarea+""].length)						arrTags["" + textarea + ""] = new Array();										arrTags["" + textarea + ""]["" + code + ""] = code;									}				else				{					selection.setSelectionText(bbcodeclose);				}			}			else			{				selection.setSelectionText(bbcode);			}					}		else if(hasCloseTag && (code == 'a href'))		{			switch(code)			{				case 'a href':					var arrData = getLink();					if (arrData[0] && arrData[1])					{						bbcode = '<'+ code +'="http://' + arrData[0] + '" target="_blank">'+ arrData[1] +'</a>';						selection.setSelectionText(bbcode);					}					break;			}		}		else		{			selection.setSelectionText(bbcode);		}	}	else	{		if(hasCloseTag  && code != 'a href')		{			selection.setSelectionText(bbcode + selection.getSelectionText() + bbcodeclose);		}				else if(hasCloseTag && (code == 'a href'))		{			switch(code)			{				case 'a href':					var arrData = getLink();					if (arrData[0] && arrData[1])					{						bbcode = '<'+ code +'="http://' + arrData[0] + '" target="_blank">'+ arrData[1] +'</a>';						selection.setSelectionText(selection.getSelectionText() + bbcode);					}					break;			}		}		else		{			selection.setSelectionText(selection.getSelectionText() + bbcode);		}	}}function searchText(textarea, strURL){	var selection = new Selection(textarea);	var searchText = "";		if (selection.selectionLength == 0)	{		searchText = prompt("Digite a palavra a ser consultada", "");		if (searchText == null)		{			return;		}	}	else	{		searchText = selection.getSelectionText();	}	strURL = strURL.replace('%s', escape(searchText));		window.open(strURL, '_blank');		}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Primeiro você varre o codigo e coloca em um array as tags de abertura.

Para validar tags fechadas, você varre o codigo e coloca em um outro array as tags fechadas.

 

1- se tiver menos elementos em um você mostra o erro

2- verifica se array1 é a tag de fechamento da tag array2[array2.length-i]

 

teoricamente é assim. Se tiver tempo eu olho como fazer isso se você n conseguir.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Acho que esse que é o código de abertura é fechamento das tags

function insertCode(textarea, code, hasCloseTag)

{

var bbcode = "<" + code + ">";

var bbcodeclose = "</" + code + ">";

 

txt = document.getElementById(textarea);

 

var selection = new Selection(textarea);

Esses são os links que determinam se vai ser negrito ou itálico

<a href="java script:insertCode('Text','b', true)" class="fonte11" title="Adicionar Negrito no texto"><b>Negrito</b></a>

<a href="java script:insertCode('Text','i', true)" class="fonte11" title="Adicionar Itálico no texto"><em>Itálico</em></a>

 

Até ai acho queta bl... só não entendi (coloca em um array as tags de abertura) O que é um array? Seria uma variável do Tipo INT que contem um valor inteiro?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Mas isso é só se tiver texto selecionado.Ele não faz verificação de nada.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.