【发布时间】:2013-08-12 17:01:16
【问题描述】:
也许这次会成功,让我们试试吧……
好吧,这是我的问题,
此代码当前有效:
$(".threadTitle").each(function() {
var colorBackground = "#F7F2ED";
if ($(this).html().indexOf("Abraham") != -1) {
$(this).parent().parent().parent().parent().css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row1").css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row2").css("background", colorBackground);
}
});
这是我正在尝试做但不起作用的:
var titles = ["Abraham"];
$(".threadTitle").each(function() {
var colorBackground = "#F7F2ED";
var realTitle = $(this).html();
if (titles.indexOf(realTitle) > -1) {
$(this).parent().parent().parent().parent().css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row1").css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row2").css("background", colorBackground);
}
});
@Jason P 提供的解决方案
var realTitle = $.trim($(this).text());
for (var i = 0; i < titles.length; i++) {
if (realTitle.indexOf(titles[i]) > -1) {
$(this).parent().parent().parent().parent().css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row1").css("background", colorBackground);
$(this).parent().parent().parent().parent().find(".row2").css("background", colorBackground);
break;
}
}
我找到的解决方案
var wordsOrPhrases = ["Abraham", "how are you"];
$(".threadTitle").each(function() {
var realTitle = $(this).text();
var asdasdasd = wordsOrPhrases.filter(function(x) {
return realTitle.indexOf(x) > -1;
}).length > 0;
if (asdasdasd) {
$(this).css("background", "#F7F2ED");
}
});
小提琴和速度测试
http://jsperf.com/testestringinsidearray
感谢所有帮助过我的人。
【问题讨论】:
-
如果您详细描述“不起作用”的含义,它可以帮助人们帮助您。是否报告了错误?有任何事情发生吗?
-
添加了一些图片来帮助解释它...
标签: javascript jquery html