我的问题提前提出:
如何过滤出应该以不区分大小写的方式匹配的元素?
相关信息:
Answer to "jquery find element by text"
我正在写一个用户脚本来过滤来自英语聊天室的西班牙语信息,在这个聊天室里,西班牙语用户开始频繁交谈,而管理行为并不在我的控制之下。
这里是DOM:
<div class="chat_message_window" style="height:366px">
<div>
<div>
<p class=""><span username="TheEnigmaTNG" class="username chat_message_window_username">TheEnigmaTNG</span><span class="separator">: </span><span class="message hyphenate">del mejor se aprende gracias a luftangreifer el mejor para mi youtuber de contract si quieren busquenlo y suscribanse</span><span class="clear"></span>
</p>
</div>
<div>
<p class="even"><span username="TheEnigmaTNG" class="username chat_message_window_username">TheEnigmaTNG</span><span class="separator">: </span><span class="message hyphenate">se los recomiendo es ingles pero no importa</span><span class="clear"></span>
</p>
</div>
</div>
</div>到目前为止,这是我的代码,它非常适合区分大小写的问题:
//to be filled with JQuery objects containing elements that contain
//Spanish text
var matches = [];
//test elements for Spanish words
function test(elem, callback){
var test = ['si','hola','de','es','bien','jaja','para','por','la','tu','qe','quien','alguien','en','soy','un','mucho','da','con','viva','mierda','cuenta','habre','vergas','ti','que','madre'];
for (var i in test){
var str = test[i];
//this is the key - it finds everything lowercase but misses
//non-lowercase words
matches.push(elem.filter(":contains('" + str + "')"));
}
callback();
}
//run function each time the chat message window is updated
$('.chat_message_window').on('DOMSubtreeModified', function () {
var messages = $(this).find('p').find('.message');
test(messages, function(){
console.log("Testing matches...");
for (var i in matches){
matches[i].parent().css("color","red");
}
});
});
//update DOM tree for testing
setTimeout(function(){
$('.chat_message_window').append('<div>UPDATE!!!</div>');
},2000);我的目标是使它不区分大小写。
我想我可以使用.filter()这样的函数:
Matches.push(函数(){返回$(this).text() == str}));
但这一点甚至不太正确。记住,elem是一个包含一组元素的JQuery对象。
如何过滤出应该以不区分大小写的方式匹配的元素?
发布于 2014-10-07 21:25:05
最新情况:
//this is the key - it finds everything lowercase but misses
//non-lowercase words
matches.push(elem.filter(":contains('" + str + "')"));致:
//Checks if the jquery object's html, converted to all lower case contains the lowercase string.
// If it does, push the element into matches
if (elem.html().toLowerCase().indexOf(str) > -1) {
matches.push(elem);
}应该做的是,检查元素html,转换为小写,为您的键字符串。
https://stackoverflow.com/questions/26245438
复制相似问题