[英]How do I select a span containing a specific text value, using jquery?
如何找到包含文本“FIND ME”的跨度
<div>
<span>FIND ME</span>
<span>dont find me</span>
</div>
http://api.jquery.com/contains-selector/
$("span:contains('FIND ME')")
预计到达时间:
包含选择器很好,但如果可能更快地过滤跨度列表: 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
使用包含:
$("span:contains('FIND ME')")
顺便说一句,如果您想将它与变量一起使用,您可以这样做:
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
});
}
}
我认为这会奏效
var span;
$('span').each(function(){
if($(this).html() == 'FIND ME'){
span = $(this);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.