【发布时间】:2018-06-11 04:43:24
【问题描述】:
我正在尝试使用 jQuery 的 inArray 函数来确定字符串是否包含数组中的单词,此处显示的是 https://stackoverflow.com/a/18867667/5798798
在我下面的示例中,它应该在控制台上打印两次“hi”,因为单词“Hello”在字符串中出现了两次并且在数组中,但它没有。
var array = ["Hello", "Goodbye"];
a = document.getElementsByClassName("here");
for (i = 0; i < a.length; i++) {
itag = a[i].getElementsByTagName("i")[0];
if (jQuery.inArray(itag.innerHTML, array) !== -1) {
console.log('hi');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="here"><i><a href="link.php">Hello</a> | <a href="link2.php">Example</a></i>
</div>
<div class="here"><i><a href="link.php">Hey</a> | <a href="link2.php">Hello</a></i>
</div>
【问题讨论】:
-
逻辑是倒退的。需要遍历数组并根据较长的字符串检查每个单词。您正在尝试查看数组中是否存在长 html 字符串
-
您需要遍历斜体内的 a 元素...并检查它们的 innerhtml
标签: javascript jquery html arrays