2011-03-11 7 views
6

Probé lo siguiente en FF, OP, Chrome, Safari e IE. Funciona en todos ellos, excepto los 3 IEs he probado: 8, 7 y 6.jQuery .trim() Pregunta de compatibilidad del navegador IE

// truncate testimonial 
var visiblePara = $('div.bannerUnder p.show'); 
if (visiblePara.text().trim().length > 150) { 
    var text = visiblePara.text().trim(); 
    var author = $('div.bannerUnder p.show > strong').text(); 
    text = text.substr(0, 150) + "..."; 
    visiblePara.text(text).append("<strong>" + author + "</strong>"); 
} 

Dice:

objeto no admite esta propiedad o método y puntos de esta línea:

if (visiblePara.text().trim().length > 150) { 

¿Cuál podría ser el problema?

+0

@Patrick : 'length' es una propiedad, no una función. –

+0

@Chuck: Cambió el título a "jQuery .length Pregunta de compatibilidad del navegador IE", pero esto no se trata de .length - se trata de .trim() no disponible en IE antes de la versión 8. – rsp

+0

Buena captura. Cambiaré la función a .trim(), pero trato de dejar el título de la pregunta en el contexto de cómo lo estaba pidiendo inicialmente. –

Respuesta

19

pruebe a cambiar:

visiblePara.text().trim().length 

a:

$.trim(visiblePara.text()).length 

Puede incluso mover la variable de texto hacia arriba, con algo como esto:

// truncate testimonial 
var visiblePara = $('div.bannerUnder p.show'); 
var text = $.trim(visiblePara.text()); 
if (text.length > 150) { 
    var author = $('div.bannerUnder p.show > strong').text(); 
    text = text.substr(0, 150) + "..."; 
    visiblePara.text(text).append("<strong>" + author + "</strong>"); 
} 
+1

gracias por salvarme la vida! – James

4

trim no es un método de String.prototype hasta IE 8. Ha existido en otros navegadores desde hace un tiempo.

Lo probé en IE8 y funcionó para mí. Utilice jQuery.trim()jQuery.trim(str) lugar

Cuestiones relacionadas