- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试构建基本的 JavaScript 搜索功能。所以我有几个 div 和一个输入搜索字段:
<div class="notes">
<div class="firstNote">FIRST NOTE</div>
<div class="secondNote">SECOND NOTE</div>
<div class="thirdNote">THIRD NOTE</div>
<div class="fourthNote">THIRDDD NOTE</div>
<div class="fifthNote">THIRL NOTE</div>
</div>
<div class="search">
<input type="text" placeholder="Search...">
</div>
这个想法是,当用户在搜索框中键入内容时,它会根据父 notes
div 内的 div 的 innerHTML
值检查他们键入的内容。当这些值匹配时,它应该将 searched
类应用于找到匹配值的那些 div。然后通过设置 display:none
来隐藏所有其他 div。一旦停止找到匹配,它应该从所有 div 中删除 searched
类,并通过给它们 display:block
再次显示它们。
问题是这仅适用于它找到的第一个匹配的 div。我创建了其中几个以字母“THIR”开头的用于测试。如果我开始输入“thir”,它只会显示(即应用“searched”类)第一个文本匹配的 div(带有 thirdNote
类的 div),但不会显示其他两个他们仍然匹配。 JS代码如下:
const notes = document.querySelectorAll(".notes div");
const search = document.querySelector(".search input");
let count = 0; //detecting the number of times the user typed in the input field
search.addEventListener("keyup", function(e){
let x = 1; //will use this to compare the values of the search box and the divs
if(e.which === 16) //if the user presses Shift, don't do anything
return;
if(e.which !== 8) //if pressed anything but Backspace, increase the count
count++;
else if(e.which === 8 && count >= 1) { // otherwise decrease the count
count--;
}
const textSearched = this.value; //what the user typed in the searchbox
//if there is no text in the search box, remove the 'searched' class from all the divs, show all of them, and reset x back to 1
if(count === 0 || textSearched === ""){
notes.forEach(function(el){
if(el.style.display === "none")
el.style.display = "block";
if(el.classList.contains("searched"))
el.classList.remove("searched");
});
x = 1;
}
notes.forEach((body) => {
//while the search box and the div text values match, and x is not greater than the div text length, add the 'searched' class and increment x
while(textSearched.substring(0, x).toLowerCase() === body.innerHTML.substring(0, x).toLowerCase() && x <= body.innerHTML.length) {
body.classList.add("searched");
x++;
//if count is less than x, it means there is a match. So show all the matching divs, and hide all the other ones
if(count < x) {
notes.forEach((el) => {
if(!el.classList.contains("searched"))
el.style.display = "none";
else
el.style.display = "block";
});
}
// if count is equal or greater than x, it means no more matching, so show all the divs and remove the 'searched' class
else {
notes.forEach(function(el){
if(el.style.display === "none")
el.style.display = "block";
if(el.classList.contains("searched"))
el.classList.remove("searched");
});
}
}
});
});
这是一个有效的 JS Fiddle:https://jsfiddle.net/tpxd5cqm/4/
谢谢!
最佳答案
这就是我要做的:当检测到匹配的搜索元素时切换父容器上的类,并在没有匹配项或输入为空时删除该类。当父类处于事件状态时,突出显示所有 .searched
子类,并隐藏所有非 .searched
子类:
const container = document.querySelector('.notes');
const notes = [...container.children];
const input = document.querySelector('input');
input.addEventListener('keydown', () => {
// Wait for the input field to contain its final value:
setTimeout(checkInput);
});
const checkInput = () => {
const value = input.value.toLowerCase();
const matchingNotes = notes.filter(note => note.textContent.toLowerCase().includes(value));
if (!value || matchingNotes.length === 0) {
container.classList.remove('searchActive');
return;
}
container.classList.add('searchActive');
for (const note of notes) {
if (matchingNotes.includes(note)) {
note.classList.add('searched');
} else {
note.classList.remove('searched');
}
}
}
.searchActive > .searched {
background-color: yellow;
}
.searchActive > *:not(.searched) {
display: none;
}
<div class="notes">
<div>FIRST NOTE</div>
<div>SECOND NOTE</div>
<div>THIRD NOTE</div>
<div>THIRDDD NOTE</div>
<div>THIRL NOTE</div>
</div>
<div>
<input type="text" placeholder="Search...">
</div>
关于javascript - 使用 keyup 事件将更改动态应用到多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59111117/
我创建了一个 keyup() 函数,代码在这里 $(document).ready(function() { var scntDiv = $('#add_words'); var wo
我希望它只在 keyup 而不是之前触发。有人可以帮忙吗? $(function() { $('#acf-field_5a32085c7df98-field_5a3208f87df99').on('k
在 Chrome 30.0.1599.101 和 IE 11 中,输入选择器无法在 Firefox 25.0.1 中正常工作 在 Chrome/IE 中损坏: $(document).ready(fu
在我为工作而构建的网站上,我们有一个搜索栏,用户可以在其中输入内容以搜索存储在一组元素数据中的各个字段。当他们开始输入时,问题出现在较慢的浏览器中。 我尝试做的是以下操作,使用 jQuery Defe
我正在尝试使用 vanilla js 编写一些简单的东西来在用户按下回车键时触发一个函数。我在下面找到了一些代码,但我不确定如何让它工作,因为该函数没有名称。 function (event) {
场景如下: 我有一个内容可编辑的 div。我还有一个文本区域,其中 .keyup() 上发生了一些事情。 我需要这样,在content editable div的keyup()上,将contented
有一个输入文本会在“onkeyup”事件上触发一个函数。当该函数触发时,我将使用 .on("keyup") 方法绑定(bind)一个“keyup”事件,以获取最后按下的键以查明它是否是字母数字,以便触
我想知道如何对同一个 #id 执行 .keyup() 和 .click() ? 即本质上,我想在用户尝试点击 enter 或点击 #search 按钮时验证 #id 。 非常感谢 最佳答案 $('#f
我必须在这里使用jquery keyup来检测用户是否输入任何文本,但我的问题是如果用户使用鼠标粘贴文本,它将无法检测到它,你能告诉我原因吗? 这是我的源代码: $("textarea").keyup
我正在尝试创建一个输入字段,用户只能在其中输入数字。这个功能对我来说已经可以正常工作了: $('#p_first').keyup(function(event){ if
我正在尝试编写类似于“占位符”polyfill 的内容。我想捕获输入字段上的 keyup 事件并获取用户输入的字符,所以我有以下代码: $elem.on('keyup',function(e){
我想我已经弄清楚了......请参阅我的评论。抱歉。 我有一个 JSON Web 服务,它在某个 keyup() 事件上调用,该事件根据输入检索一些数据。我想在输入任何内容之前加载一组初始数据。我正在
所以我使用 Keyup 来计算输入字段的总数。到目前为止,一切都很好。但现在我想这样做:如果选中#a 复选框,则将总金额加 12。如果没有,则添加 0。 我的代码基于此:http://jsfiddle
我在弄清楚为什么 keyup 事件在以下情况下不起作用(不显示警报)时遇到问题,我对表单使用 bootstrap 3,但我不认为 form-control取消键盘输入? HTML: jQuery:
我正在构建一个搜索表单,.keyup 中的 ajax 函数可以有不同的参数,我想通过检查是否存在另一个值来检查是否需要包含此参数。这是我现在拥有的: var delay = (function() {
我的 html 和 jquery 代码 Insert title here td { min-height: 50px; min-width: 50px; }
每次我按下键盘上的一个键,它都会将 $(this).val(""); 设置为 null,并且 score 变量将为 -2 . initialize: function() { var scor
我是 javascript 及其事件的新手。这是我的表格 html 代码,其中有收入标题及其各自的价格。 sn Title of income
我有一个这样的表: 当数量/价格变化小计也发生变化时,我想制作Javascript。我的js: $("#invoice_item").delega
我正在 js 中制作字符计数器,但 keyup 事件不起作用??? $(document).ready(function() { function countingCharacter(elemen
我是一名优秀的程序员,十分优秀!