- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作我的第一个搜索栏/引擎。我想让搜索栏响应用户输入,以便将它们发送到特定网站。我遇到的问题是,当用户从 <li><a> </a></li>
中的文本输入某些关键字时,我的搜索按钮不会将用户发送到网页。进入<input></input>
并按下搜索按钮。我该如何解决这个问题?
编辑:
网站:http://techteach.us/Web2020/ZWeiJian/WCP/Labs/Lab_01/Lab_1.html
//Search engine functionality
var sForm = document.getElementById("srchFrm");
document.addEventListener("click", function(event) {
var clickedInside = sForm.contains(event.target);
if (clickedInside) {
//Displaying the search suggestions
document.getElementById("srchRslts").style.display = "block";
} else {
//Hiding the search suggestions
document.getElementById("srchRslts").style.display = "none";
}
});
//Credit to W3Schools
function searchingResults() {
// Declaring variables
let input, filter, ul, li, a, i, txtValue;
input = document.getElementById('srchBar');
filter = input.value;
ul = document.getElementById("srchRslts");
li = ul.getElementsByTagName('li');
// Loop through all list items, and hide those who don't match the search query
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
//Credit to Textfixer.com and https://stackoverflow.com/questions/155188/trigger-a-button-click-with-javascript-on-the-enter-key-in-a-text-box for the search button code.
//Get the Search Button
var submitButton = document.getElementById("sbmtBtn");
//Add event listener to the submit button
input.addEventListener("keyup", function(e) {
e.preventDefault();
//Press enter to activate the search engine
if (event.keyCode === 13) {
submitButton.click();
}
});
function cSbmtBtn() {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (filter == txtValue) {
submitButton.value = txtValue;
}
}
}
<!--Basic Search Bar.-->
<form id="srchFrm">
<input id="srchBar" type="text" onKeyUp="searchingResults();" placeholder="Search...">
<button type="submit" id="sbmtBtn" value="" onClick="cSbmtBtn"><i class="fa fa-search"></i></button>
<ul id="srchRslts">
<li><a href="Lab_1.html">Home</a></li>
<li><a href="Lb1Labs.html">Labs</a></li>
<li><a href="Lb1Projects.html">Projects</a></li>
<li><a href="https://qwfepfp.blogspot.com/2019/09/blog-intromy-bio-2.html">Blogger</a></li>
<li><a href="http://techteach.us/index.html">Techteach.us</a></li>
<li><a href="http://techteach.us/Web2020/">Parent Directory</a></li>
<li><a href="http://techteach.us/index.html">Mrs. Ramirez's Site</a></li>
<li><a href="https://www.infotechhs.net/">Information Technology High School Website</a></li>
<li><a href="Lab_1Redirectory.html">Lab 1</a></li>
<li><a href="../Lab_02/Lab_2.html">Lab 2</a></li>
<li><a href="../Lab_03/Lab_3.html">Lab 3</a></li>
<li><a href="../Lab_04/Lab_4.html">Lab 4</a></li>
<li><a href="../Lab_05/Lab_5.html">Lab 5</a></li>
<li><a href="../Lab_06/Lab_6.html">Lab 6</a></li>
<li><a href="Lb1ErrorPage.html">Lab 7</a></li>
<li><a href="Lb1ErrorPage.html">Lab 8</a></li>
<li><a href="Lb1ErrorPage.html">Lab 9</a></li>
<li><a href="Lb1ErrorPage.html">Lab 10</a></li>
<li><a href="Lb1ErrorPage.html">Lab 11</a></li>
<li><a href="Lb1ErrorPage.html">Lab 12</a></li>
<li><a href="Lb1ErrorPage.html">Lab 13</a></li>
<li><a href="Lb1ErrorPage.html">Lab 14</a></li>
<li><a href="Lb1ErrorPage.html">Lab 15</a></li>
<li><a href="Lb1ErrorPage.html">Lab 16</a></li>
<li><a href="Lb1ErrorPage.html">Lab 17</a></li>
<li><a href="Lb1ErrorPage.html">Lab 18</a></li>
<li><a href="Lb1ErrorPage.html">Lab 19</a></li>
<li><a href="Lb1ErrorPage.html">Lab 20</a></li>
<li><a href="../../Projects/Pr1/index.html">Project 1</a></li>
<li><a href="../../Projects/ECONO/ECONO.html">Project 2</a></li>
<li><a href="Lb1ErrorPage.html">Project 3</a></li>
<li><a href="Lb1ErrorPage.html">Project 4</a></li>
<li><a href="Lb1About.html">About</a></li>
<li><a href="Lb1PD.html">Privacy & Disclaimer</a></li>
<li><a href="Lb1TS.html">Terms of Service</a></li>
<li><a href="Lb1Citations.html">Citation</a></li>
</ul>
</form>
最佳答案
html
<form id="srchFrm">
<input id="srchBar" type="text" placeholder="Search...">
<button type="submit" id="sbmtBtn" value="">Search</button>
<!-- Rest of form -->
</form>
脚本.js
// Search engine functionality
var sForm = document.getElementById("srchFrm");
var input = document.getElementById('srchBar');
var anchors = document.querySelectorAll("form ul li a");
var anchorTexts = anchors.map (anchor => anchor.textContent);
var matchedAnchors = [];
// Document click listener to detect clicks inside sForm
document.addEventListener("click", function(event) {
// Document click handler logic
});
// Submit handler to prevent default form submission.
// Form submission is also triggered when the submit button is clicked
// So the logic of the submit button can be moved here
sForm.addEventListener("submit", function(event) {
event.preventDefault(); // Necessary to prevent form submission which will redirect the page to the URL in the `action` attribute of the form
// Form submission logic goes here
// There can be multiple anchors matching the text, so I'll assume you want the first match
if (matchedAnchors.length === 0) return;
// Change the window location to the href of the first matched anchor
window.location.href = matchedAnchors[0].getAttribute("href")
});
// Input key up handler. Logic for what happens on key up goes here
input.addEventListener("keyup", function(event) {
var inputValue = event.target.value; // or input.value
matchedAnchors.splice(0); // Clear the matchedAnchors array before starting a new search
// Make all anchors visible before starting a new search
for (const anchor of anchors) anchor.style.display = "inline-block"
// Find anchors that match the input
for (const i = 0; i < anchorTexts.length; i++) {
const anchorText = anchorTexts[i];
if (anchorText.startsWith(inputValue)) { // Or 'indexOf' if you want a partial match starting from anywhere in the string
matchedAnchors.push (anchors[i]);
}
}
// Find anchors that match the input
for (const anchor of anchors) {
if (matchedAnchors.includes(anchor)) {
anchor.style.display = "inline-block";
} else anchor.style.display = "none";
}
})
此类内容应该可以帮助您......
关于javascript - 如何触发搜索按钮从用户输入转到网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130470/
这个问题与窗口处理或多个浏览器窗口的杂耍无关,而是关于在同一窗口中浏览 Web 应用程序的网页。我遇到这样的情况 1.我导航为屏幕 A->屏幕 x->屏幕 Y->屏幕 B 2.我需要捕获首次登录时屏幕
我有这个要求: The system will record the length of time the user displayed each page. 虽然在富客户端应用程序中微不足道,但我不
我在调试 JavaScript 网页时遇到问题。我遇到困难的地方是我标记 (...) 的地方。我收到未定义的错误。我是否将函数 countDown(start, Increment) 中的参数(即 s
需要一些帮助。我刚开始学习 HTML,今天一直在研究如何制作菜单,但在这样做时遇到了问题。 我似乎不知道如何在屏幕上居中显示菜单。 这就是我目前所拥有的; Home
我想通过单击按钮将小程序的任何参数发送到浏览器。 (HTML)。我知道按钮对象有一些方法,但不知道使用哪个。我怎样才能做到这一点?ps .: 我使用的是 jnlp 协议(protocol)。 类似于:
我应该使用Wikipedia的文章链接数据转储从组织的网站中提取代表性术语。 为此,我已经- 抓取并下载了该组织的网页。 (〜110,000) 创建了Wikipedia ID和术语/标题的字典。 (约
我的网页中包含 javascript 函数... function callFromAndroid(varName) { alert("call from android activated by
我想创建一个 Java 应用程序,允许用户导入网页并能够在程序中对其进行编辑。 导入网页将对其进行渲染,并且页面的组件(图像、文本等)将是可编辑或可拖动的,从而允许用户重新布局组件。 例如,用户可以加
当我们按下按钮时,我向 JFrame 添加了一个网页(网页在同一框架中打开)。效果很好。但我想向其中添加一个scrollPane,但是当我添加 JScrollPane jsp = new JScrol
我在使用 particles.js 时无法将图像居中。图像居中,但略微偏离中心。为什么要这样做,我如何才能将它居中? html particles.js demo CSS
我正在尝试在加载页面时播放音频,它应该非常简单但我无法完成。 问题是它没有播放,我尝试检查自动播放的状态(真/假),它说它在页面加载时播放,尽管它没有播放,还尝试制作一个将改变自动播放的功能状态为
我正在尝试显示用户从列表中选择的图像,但我在屏幕上看不到任何内容。 .container { position: relative; } .ce
这听起来有点奇怪,但我需要一些帮助,网页必须有一行必须包含三个部分,第一部分必须有 1 列的偏移量,并且部分之间的空间必须是 10px到目前为止,使用 Bootstrap 一切顺利。 现在第二行将有
这个问题在这里已经有了答案: Web and physical units (2 个答案) Div width in cm (inch) (6 个答案) 关闭 9 年前。
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想将我的 IPython 笔记本的宽度设置为 2500 像素,并且我希望它左对齐。我该怎么做? 我使用这段代码来应用我自己的 CSS: from IPython.core.display impor
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this q
我在 Word 中制作了一份文档,希望人们在其中添加自己的姓名以及他们的教学经验。我已将其保存为网页并发布到此处: http://epicforum.net/TS ...但操作部分实际上就是这样: h
这个问题在这里已经有了答案: Execute JS code after pressing the spacebar (5 个答案) 关闭 4 年前。
我正在开发一个只有两个页面的网站。 1.登录 2.首页 我正在使用 Angular 框架。 app.config(['$routeProvider', function ($routeProvider
我是一名优秀的程序员,十分优秀!