【问题标题】:Find piece of string inside array and do something if I can find it在数组中找到一段字符串,如果我能找到它就做点什么
【发布时间】: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://jsfiddle.net/aYc2d/

http://jsperf.com/testestringinsidearray

感谢所有帮助过我的人。

【问题讨论】:

  • 如果您详细描述“不起作用”的含义,它可以帮助人们帮助您。是否报告了错误?有任何事情发生吗?
  • 添加了一些图片来帮助解释它...

标签: javascript jquery html


【解决方案1】:

您的问题仍未得到很好的解释,但我想我知道了。

你在问为什么返回 true:

if($(this).html().indexOf("Title A") != -1) {
    //should get here
}

但这不是:

var titles = ["Title A"];
var realTitle = $(this).html();
if (titles.indexOf(realTitle) > -1) {
    //should get here
}

区别在于string.indexOf()array.indexOf()之间的区别。

string.indexOf() 在字符串中的任意位置搜索指定的文本。

array.indexOf() 在数组中的任何位置搜索指定的对象(不一定是字符串)。

编辑从您的图片中,我可以看到您的跨度包含很多空格。试试改成这样:

var realTitle = $.trim($(this).text());

编辑 2 回应您的评论:

在这种情况下,您需要迭代数组并对每个项目执行indexOf。我想这就是你想要的:

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;
    }
}

【讨论】:

  • 这工作......有点......假设 threadTitle 是“Hello World 12345”,我可以使用第一个代码 .indexOf("Hello World") 它会找到它,用这个第二个使用我不能使用的数组,如果我完全使用 indexOf("Hello World 12345"),它就可以工作。
  • @GabeNewell 编辑回复。
  • 我找到了另一种方法,但你的方法完美无缺,实际上速度更快,我会将它们都添加到 OP 中并包括速度测试。非常感谢!
【解决方案2】:

首先,您可能需要$(this).text() 而不是.html(),以防您有嵌套跨度或其他东西(尽管您不应该这样做)。

但这不应该导致您遇到的问题。您必须在两个示例之间的html中有所不同。

【讨论】:

  • 同 var realTitle = $(this).text().trim();但感谢您的提示,我不知道修剪可以用于其他用途。
  • @GabeNewell .trim() 并非所有浏览器都支持,因此您可能需要使用$.trim($(this).text())
  • 我认为您的两个示例之间必须有 html 差异。此外,您选择.row1.row2 的事实有点可疑。显示您的 html 会有所帮助
猜你喜欢
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多