在 <div> 中搜索文本
Search <div> for text
我试图在单个 HTML5 页面上搜索每个页面,如果没有匹配项则隐藏。我 'almost' 但还不够。
到目前为止,如果有单个字符的匹配项,我的将消失,但如果我键入其他任何内容,则会重新出现。
另外,如果我清除搜索框,我希望页面重置。如果你们 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>
给你。使用了符合您需要的 includes
方法。
function myFunction() {
var input = document.getElementById("Search");
var filter = input.value.toLowerCase();
var nodes = document.getElementsByClassName('target');
for (i = 0; i < nodes.length; i++) {
if (nodes[i].innerText.toLowerCase().includes(filter)) {
nodes[i].style.display = "block";
} else {
nodes[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>
<div class="target">
Can you find me?
</div>
您可以将 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>
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(); }
});
}
您的代码运行良好!。
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>
添加到 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>
我试图在单个 HTML5 页面上搜索每个页面,如果没有匹配项则隐藏。我 'almost' 但还不够。 到目前为止,如果有单个字符的匹配项,我的将消失,但如果我键入其他任何内容,则会重新出现。 另外,如果我清除搜索框,我希望页面重置。如果你们 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>
给你。使用了符合您需要的 includes
方法。
function myFunction() {
var input = document.getElementById("Search");
var filter = input.value.toLowerCase();
var nodes = document.getElementsByClassName('target');
for (i = 0; i < nodes.length; i++) {
if (nodes[i].innerText.toLowerCase().includes(filter)) {
nodes[i].style.display = "block";
} else {
nodes[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>
<div class="target">
Can you find me?
</div>
您可以将 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>
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(); }
});
}
您的代码运行良好!。
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>
添加到 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>