我正在尝试在单个HTML5页面上搜索EACH,然后隐藏,如果没有匹配。我“差不多”但不完全在那里。 到目前为止,如果有一个char匹配,我的意志就会消失,但如果我输入任何其他内容,我的意思就会重新出现。 另外,如果我清除搜索框,我希望页面重置。如果你的Java人员可以看看,真的很感激。
function myFunction() {
input = document.getElementById("Search");
filter = input.value.toUpperCase();
for (i=0; i<document.getElementsByClassName('target').length; i++){
if(document.getElementsByClassName('target'[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
document.getElementsByClassName("target")[i].style.display = "none";
}
else{
document.getElementsByClassName("target")[i].style.display = "";
}
}
}
</script>
<table align="center" width="20%">
<tr>
<td style="padding-right: 10px">
<input type="text" id="Search" onkeyup="myFunction()"
placeholder="Please enter a search term.." title="Type in a name">
</td>
</tr>
</table>
<br>
<div class="target">
This is my DIV element.
</div>
<div class="target">
This is another Div element.
</div>
答案 0 :(得分:3)
您的代码效果很好!
if(document.getElementsByClassName('target')<--you forgot to close paranthesis
并撤销你的if..else,
你的if..else应该是这样的
if(document.getElementsByClassName('target')[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
document.getElementsByClassName("target")[i].style.display = "block";
}
else{
document.getElementsByClassName("target")[i].style.display = "none";
}
}
function myFunction() {
input = document.getElementById("Search");
filter = input.value.toUpperCase();
var length = document.getElementsByClassName('target').length;
for (i=0; i<length; i++){
if(document.getElementsByClassName('target')[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
document.getElementsByClassName("target")[i].style.display = "block";
}
else{
document.getElementsByClassName("target")[i].style.display = "none";
}
}
}
<table align="center" width="20%">
<tr>
<td style="padding-right: 10px">
<input type="text" id="Search" onkeyup="myFunction()"
placeholder="Please enter a search term.." title="Type in a name">
</td>
</tr>
</table>
<br>
<div class="target">
This is my DIV element.
</div>
<div class="target">
This is another Div element.
</div>
答案 1 :(得分:2)
您可以将toggle
与搜索字符串结合使用,效果非常好。
$("#Search").on("keyup", function () {
val = $(this).val().toLowerCase();
$("div").each(function () {
$(this).toggle($(this).text().toLowerCase().includes(val));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table align="center" width="20%">
<tr>
<td style="padding-right: 10px">
<input type="text" id="Search"
placeholder="Please enter a search term.." title="Type in a name">
</td>
</tr>
</table>
<br>
<div class="target">
This is my DIV element.
</div>
<div class="target">
This is another Div element.
</div>
答案 2 :(得分:1)
Using JQuery: https://jquery.com
function myFunction() {
input = document.getElementById("Search");
filter = input.value.toUpperCase();
//Show all div class target
$("div.target").show();
//All div class target that not contains filter will be hidden
$('div.target').each(function(index, elem){
if($(elem).text().toUpperCase().includes(filter)) { //Not hidden
} else { $(elem).hide(); }
});
}
答案 3 :(得分:1)
添加到Oen44的答案中-如果要在未在任何搜索到的元素中找到查询时添加“未找到结果”消息,则可以这样做:
JavaScript:
function searchFunction() {
var input = document.getElementById("Search");
var filter = input.value.toLowerCase();
var nodes = document.getElementsByClassName('target');
var itemsDisplayed = 0;
document.getElementById('no_results').display = "none";
for (i = 0; i < nodes.length; i++) {
if (nodes[i].innerText.toLowerCase().includes(filter)) {
nodes[i].style.display = "block";
itemsDisplayed += 1;
} else {
nodes[i].style.display = "none";
}
}
if (itemsDisplayed === 0){
document.getElementById('no_results').display = "block";
}
}
HTML:
<table align="center" width="20%">
<tr>
<td style="padding-right: 10px">
<input type="text" id="Search" onkeyup="myFunction()"
placeholder="Please enter a search term.." title="Type in a name">
</td>
</tr>
</table>
<br>
<div class="target">
This is my DIV element.
</div>
<div class="target">
This is another Div element.
</div>
<div class="target">
Can you find me?
</div>
<div id="no_results" style="display:none;">
No results found
</div>