Tengo una cadena que contiene texto y algunas tags ; Quiero saber cómo puedo seleccionar una etiqueta de la variable y hacer un bucle. Intenté lo siguiente pero no funcionó:
var text = `some string here with http:something.com more string and more links also`; $('a', text).each(function() { var string = $(this).html(); $(this).html(string.substring(0, length-1)+(string.length > length ? end : '')); });
Necesitas envolver el texto en un div (u otro elemento) y luego find()
:
var text = 'some string here with http:something.com more string and more links also'; text = $('' + text + ''); text.find('a').each(function() { var length = 10; var end = '...'; var string = $(this).html(); $(this).html(string.substring(0, length) + (string.length > length ? end : '')); }); var text = text.html(); // Put it into a textarea $('#myTextarea').val(text);
Reemplazar
$('a', text).each(function() {
con
$(text, 'a').each(function() {
y ver si funciona.