Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
getSelection() no funciona en IE

¿Puede alguien ayudarme a hacer que este código funcione en IE, por favor?

HTML:

 <p>Alex Thomas</p> <button id="click">Click</button>

JS

 $('#click').click(function(){ var range = window.getSelection().getRangeAt(0); var selectionContents = range.extractContents(); var span = document.createElement("span"); span.style.color = "red"; span.appendChild(selectionContents); range.insertNode(span); });

Codificado aquí: http://jsfiddle.net/WdrC2/

Gracias por adelantado...

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

IE anterior a 9 no es compatible window.getSelection() . En su lugar, puede usar document.selection (consulte esta página de msdn para ver la descripción). Este objeto de selección tiene un método createRange() que devuelve un objeto TextRange (consulte esta página de msdn para obtener más detalles).

Tenga en cuenta que tanto los objetos de selection como los de textrange de texto son una implementación propia de Microsoft y no siguen los estándares del W3C. Puede leer más sobre el rango de texto y los problemas de range en textrange .

La siguiente implementación funciona en IE:

 $('#click').click(function(){ var range = document.selection.createRange(); range.pasteHTML("<span style='color: red'>" + range.htmlText + "</span>"); });

No es tan bueno como la otra implementación ya que tienes que trabajar con cadenas en lugar del dom. Hay un pequeño truco en el que pega <span id="myUniqueId"></span> como marcador de posición y luego lo reemplaza usando el dom. Sin embargo, todavía tiene que trabajar con range.htmlText o range.text .

Por cierto: la implementación anterior es obviamente solo para IE. Tiene que usar alguna capacidad de detección del navegador para decidir qué versión usar.

over 4 years ago · Santiago Trujillo Relatório

0

Pruebe este aquí: http://jsfiddle.net/6BrWe/

Es un poco complicado y no tan bonito, pero debería funcionar en IE y otros navegadores. Sin embargo, no he hecho muchas pruebas en varios navegadores :)

 $('#click').click(function() { var whatBrowser = getIt(); if (whatIsIt == 'notIE' && whatBrowser) { notIE(whatBrowser); } else if (whatIsIt == "isIE"&& whatBrowser) { isIE(whatBrowser); }; }); var whatIsIt = ""; function getIt() { if (window.getSelection) { whatIsIt = "notIE"; return window.getSelection(); } else if (document.getSelection) { whatIsIt = "notIE"; return document.getSelection(); } else { var selection = document.selection && document.selection.createRange(); if (selection.text) { whatIsIt = "isIE"; return selection; }; return false; }; return false; }; function isIE(selection) { if (selection) { var selectionContents = selection.text; if (selectionContents) { selection.pasteHTML('<span class="reddy">' + selectionContents + '</span>'); }; }; }; function notIE(selection) { var range = window.getSelection().getRangeAt(0); var selectionContents = range.extractContents(); var span = document.createElement("span"); span.className= "reddy"; span.appendChild(selectionContents); range.insertNode(span); };
over 4 years ago · Santiago Trujillo Relatório

0

Si quieres colorear "Alex Thomas" de rojo, puedes hacerlo

HTML

 <p id='txt'>Alex Thomas</p> <input type='button' id='btn' value='click' />

JS

 $('#click').click(function(){ $('#txt').attr('color','Red'); });
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda