我正在为我的网站创建一个jQuery搜索脚本,但是我收到以下错误:
Uncaught TypeError: $(...)[index].hide is not a function (search.js:9)
Uncaught TypeError: $(...)[index].show is not a function (search.js:7)
Run Code Online (Sandbox Code Playgroud)
我知道是什么导致它,但我只是不知道为什么.我有一个包含多个tr元素的表,每个元素都以某种方式包含我想要搜索的"mod"的名称.这是我的搜索脚本(search.js):
var keystroke = 0;
$( "#simpleSearch" ).on("keyup", function() {
keystroke = keystroke + 1;
$.each($(".mod"), function(index, value) {
var searchQuery = document.getElementById("simpleSearch").value;
if (searchQuery == "") {
$(".mod")[index].show();
} else {
$(".mod")[index].hide().filter(function() {
return value.children[0].children[0].value.toLowerCase().includes(searchQuery.toLowerCase());
})
}
})
})
Run Code Online (Sandbox Code Playgroud)
这是我想要搜索的表格:
<table class="table table-hover table-versions-hover modlist">
<thead>
<tr>
<th>
Mod Name
</th>
<th>
Author(s)
</th>
</tr>
</thead>
<tbody>
<tr class="mod">
<th>
<a href="Mod%20Link" …
Run Code Online (Sandbox Code Playgroud) 我有一个数字(比方说 525)。我想把这个数字分成一个块数组,每个值最多 100 个。如果我把525
它拆分成一个数组,它看起来像:
[
100,
100,
100,
100,
100,
25
]
Run Code Online (Sandbox Code Playgroud)
这是我迄今为止尝试过的:
var number = 525;
var array = [];
while (number > 0) {
number = number - 100;
array.push(Math.min(number, 100));
}
Run Code Online (Sandbox Code Playgroud)
那不会让我走远。它只是返回[ 100, 100, 100, 100, 25, -75 ]
。我知道使用while
不是最好的方法,但这是我能想到的。有没有人有其他方法可以改进我的代码并提高效率?