Javascript 如何使用jquery选择包含特定文本值的范围?
如何找到包含文本“find ME”的范围Javascript 如何使用jquery选择包含特定文本值的范围?,javascript,jquery,Javascript,Jquery,如何找到包含文本“find ME”的范围 找到我 别找我 预计到达时间: contains选择器很好,但如果可能更快,则会过滤跨距列表: 使用: 我想这会管用的 var span; $('span').each(function(){ if($(this).html() == 'FIND ME'){ span = $(this); } }); 顺便说一句,如果您想将其用于变量,您可以这样做: function findText() { $('span').css('
找到我
别找我
预计到达时间:
contains选择器很好,但如果可能更快,则会过滤跨距列表:
使用:
我想这会管用的
var span;
$('span').each(function(){
if($(this).html() == 'FIND ME'){
span = $(this);
}
});
顺便说一句,如果您想将其用于变量,您可以这样做:
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
});
}
}
这回答了我的下一个问题吗?对Filter再次是答案,但这次您将检查整个.text()值是否匹配,而不是查找索引。答案更新。谢谢Malk!我没听说过filter()。有时这是必要的,因为contains()是一个通配符比较,因此将查找“find ME”和“DON'T find ME”,如果您需要一个精确的匹配项,这对您没有帮助。可能的重复项是如何在Python中使用selenium执行此操作?这不起作用。在OP中,他有两个跨度:
找到我
,和不找到我
。您正在检查跨度是否包含此文本,因此您找到了这两个文本。
$("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')")
var span;
$('span').each(function(){
if($(this).html() == 'FIND ME'){
span = $(this);
}
});
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
});
}
}