Ir para conteúdo

Arquivado

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

benck

Função selectionStart em IE

Recommended Posts

Olá pessoal, estou fazendo uma função e tive problema com o IEO que quero fazer é saber em que posição está o cursor como o script ai em baixo faz, só que ele funciona no FF e não no IEDei uma olhada em outros tópicos, mas só preciso que esse código funcione o IE<script language="JavaScript" type="text/JavaScript"> function informaposicao() { var posicao = document.formulario.texto.selectionStart; alert(posicao); }</script><form name="formulario"><center><input name="botao" type="button" value="Ver posição" onClick="informaposicao();"><br><br>Texto:<br><br><textarea name="texto" id="texto" cols="30" rows="10"></textarea><br></center></form>Valeu!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá pessoal, estou fazendo uma função e tive problema com o IEO que quero fazer é saber em que posição está o cursor como o script ai em baixo faz, só que ele funciona no FF e não no IEDei uma olhada em outros tópicos, mas só preciso que esse código funcione o IE<script language="JavaScript" type="text/JavaScript"> function informaposicao() { var posicao = document.formulario.texto.selectionStart; alert(posicao); }</script><form name="formulario"><center><input name="botao" type="button" value="Ver posição" onClick="informaposicao();"><br><br>Texto:<br><br><textarea name="texto" id="texto" cols="30" rows="10"></textarea><br></center></form>Valeu!!!

Caro parceiro, estou também tentando resolver este problema. Conseguindo te informo. Mas se alguém conseguir primeiro, me avise também

Compartilhar este post


Link para o post
Compartilhar em outros sites

pesquise por document.selection.createRange();espero ter ajudadoAbracoFabio Morikawa

Compartilhar este post


Link para o post
Compartilhar em outros sites

Depois de muito quebrar a cabeça, desenvolvi este script:

 

<html>
	<head>
		<title>Exemplo</title>
<script type="text/JavaScript">
function FormatarTexto(campo,tipo) {
	var txtCopia = document.getElementsByName(campo)[0];
	var selectedText = "";
	selectedText = txtCopia.value.substring(txtCopia.selectionStart, txtCopia.selectionEnd);
	if(txtCopia.selectionStart){ // FF
		txtCopia.value = txtCopia.value.substring(0, txtCopia.selectionStart) + "<" + tipo + ">" + selectedText + "</" + tipo + ">" + txtCopia.value.substring(txtCopia.selectionEnd, txtCopia.value.length);
	} else if(document.selection){ // IE
		var rangeIE = document.selection.createRange();
		selectedText = rangeIE.text;
		if(selectedText){
			rangeIE.text = "<" + tipo + ">" + selectedText + "</" + tipo + ">";
		}
	}
}
</script>
	</head>
	<body>
	<p>Texto qualquer:<br>
	  <input type="text" name="noticia_texto" />
	</p>
	<p><strong><a href="#" onClick="FormatarTexto('noticia_texto','b');">ENVIAR</a></strong></p>
	</body>
</html>

A única coisa que não consegui foi inserir pelo IE o rangeIE de volta no campo quando nada é selecionado. Alguém pode me dizer como?

 

Alexandre.

www.fullblog.com.br

Compartilhar este post


Link para o post
Compartilhar em outros sites

<html>

<head>

<title>Exemplo</title>

<script type="text/JavaScript">

adicionaEvento(window, 'load', function(){

adicionaEvento(document.getElementsByName("b1")[0], 'click', function(){

var txC = document.getElementsByName("txq")[0];

var sS,sE;

if(txC.selectionEnd){

sS = txC.selectionStart;

sE = txC.selectionEnd;

} else if(document.selection){

var range = document.selection.createRange();

if(range.parentNode == txC){

sS = range.startContainer

sE = range.endContainer;

}

}

alert("Inicio da selecao:" + sS + "\nFim da selecao:" + sE + "\nQuantidade de caracteres da selecao:" + (sE-sS));

}, false);

}, false);

 

function adicionaEvento(elemento, evento, funcao, bool){

bool = (bool == null)? false : bool;

if(elemento.addEventListener)

elemento.addEventListener(evento, funcao, bool);

else

elemento.attachEvent('on' + evento, funcao);

}

 

</script>

</head>

<body>

Texto qualquer:<input type="text" name="txq" /><br />

<input type="button" value="Pegar texto selecionado" name="b1" />

</body>

</html>

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.