知识点1
str.indexOf("") ==> 0; != -1; 就是全选效果
str.indexOf(“content”) != -1 表示在str中,就添加到列表
知识点2 AJAX
知识点3 数组的 forEach
getData("");
button.onclick = function () {
var str = input_search.value;
getData(str);
}
function getData(str) {
AJAX(
{
url: "../static/hero.json",
data: {
},
success: function (obj) {
var html = "";
obj.forEach(function (item) {
if (item.hurt.indexOf(str) != -1 || item.name.indexOf(str) != -1 || item.location.indexOf(str)!= -1) {
html +=
`<li>
<div class="img_box">
<img class="img" src="../images/${item.src}" alt="">
<div class="msg_box">
<p class="name">${item.name}</p>
<p class="location">${item.location}</p>
<p class="hurt">${item.hurt}</p>
</div>
</div>
</li>`;
}
});
// console.log(html);
innerBody.innerHTML = html;
},
error: function () {
}
}
);
}