【发布时间】:2016-02-06 22:54:23
【问题描述】:
我正在尝试设置一个搜索,该搜索在单个 html 文件中为我提供多个搜索结果。假设 html 包含以下摘录:
<input id="search" type="text">
<div class="chapter" id="chap1">
<h1>Chapter 1 - Introduction</h1>
<p>Lorem Ipsum. Lorem Lorem Ipsum</p>
</div>
<div class="chapter" id="chap2">
<h1>Chapter 2 - Say hello</h1>
<p>Lorem Ipsum. Ipsum Lorem Ipsum</p>
</div>
<div class="chapter" id="chap3">
<h1>Chapter 3 - Say bye</h1>
<p>Ipsum Ipsum. Ipsum Ipsum Ipsum</p>
</div>
如果我现在要搜索“Lorem”,我想获得包含“Lorem”的“章节”类的每个 div 的结果(= 第一个和第二个 div = 2 个搜索结果)。每个搜索结果应该只是一个链接,内容为 h1 并在点击时转到相应的 div。
例子:
<p>Your search has returned 2 results.</p>
<a>Chapter 1 - Introduction</a>
<a>Chapter 2 - Say hello</a>
理想情况下,在我删除搜索文本或单击其中一个结果之前,不应显示原始内容。
非常感谢!非常感谢您的帮助!
【问题讨论】:
-
在您的 javascript 函数中创建链接,该链接具有 href="#chap1"(或您想要与之关联的任何其他 ID)。我建议为此使用 jQuery,因为您可以使用 CSS 选择器轻松导航。 beski.wordpress.com/2009/04/21/…
-
示例输出中的链接在哪里?