¿Cómo encuentro el lapso que contiene el texto "FIND ME"¿Cómo selecciono un tramo que contiene un valor de texto específico, usando jquery?
<div>
<span>FIND ME</span>
<span>dont find me</span>
</div>
¿Cómo encuentro el lapso que contiene el texto "FIND ME"¿Cómo selecciono un tramo que contiene un valor de texto específico, usando jquery?
<div>
<span>FIND ME</span>
<span>dont find me</span>
</div>
http://api.jquery.com/contains-selector/
$("span:contains('FIND ME')")
ETA:
El contiene selector es agradable, pero filtrar una lista de períodos si así probablemente más rápido: http://jsperf.com/jquery-contains-vs-filter
$("span").filter(function() { return ($(this).text().indexOf('FIND ME') > -1) }); -- anywhere match
$("span").filter(function() { return ($(this).text() === 'FIND ME') }); -- exact match
¿Eso responde mi siguiente pregunta? http://stackoverflow.com/questions/9424509/how-do-i-select-a-span-containing-an-exact-text-value-using-jquery – MedicineMan
Sí. El filtro es la respuesta de nuevo, pero esta vez comprueba el valor completo de .text() para una coincidencia en lugar de buscar el índice. Respuesta actualizada – Malk
Gracias Malk! No había oído hablar de filter(). A veces es necesario ya que contains() es una comparación comodín, por lo que encontrarás 'ENCUÉNTRAME' y 'NO ME ENCUENTRE', lo que no es útil si necesitas una coincidencia exacta – Peadar
Uso contains:
$("span:contains('FIND ME')")
creo que esto va a funcionar
var span;
$('span').each(function(){
if($(this).html() == 'FIND ME'){
span = $(this);
}
});
Oh, sí. Esas otras respuestas son mucho mejores. Usa esos. – buck54321
Por cierto, si desea utilizar esto con una variable, que haría de esta manera:
function findText() {
$('span').css('border', 'none'); //reset all of the spans to no border
var find = $('#txtFind').val(); //where txtFind is a simple text input for your search value
if (find != null && find.length > 0) {
//search every span for this content
$("span:contains(" + find + ")").each(function() {
$(this).css('border', 'solid 2px red'); //mark the content
});
}
}
posible duplicado de [jquery find element by text] (http://stackoverflow.com/questions/7321896/jquery-find-element-by-text) –