[英]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.