在同一页面上进行HTML搜索 [英] HTML Searching on the same page

查看:48
本文介绍了在同一页面上进行HTML搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一整页的内容.如果我想创建一个搜索栏,以便当用户搜索单个页面上某处存在的内容时,它将它们重定向到页面的该部分(就像它转到页面的中间或底部).这需要像PHP这样的后端吗?还是可以只使用HTML?

I have one long page of content. If I want to create a search bar so that when the user searches something that exists somewhere on the single page, it redirects them to that part of the page (like it goes to the middle or the bottom of the page). Does this require any back-end like PHP or can I do this with just HTML?

推荐答案

您可以在JavaScript中使用 window.find()
参考: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/find

You can make use of window.find() in JavaScript
Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/find

演示:

function search(string){
window.find(string);
}

<input placeholder="type foo or bar" type="text" id="search">
<input type="button" value="Go" onclick="search(document.getElementById('search').value)">
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
<p>foo</p>
<p>bar</p>
<p>hello world</p>

这篇关于在同一页面上进行HTML搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆