【发布时间】:2016-09-02 13:48:55
【问题描述】:
我正在尝试编写一个 jQuery 函数,该函数将搜索页面上包含多个类的 url 的列表。我希望该函数使用输入到搜索框中的单词来匹配包含与输入内容匹配的类的列表项,并仅显示这些列表项。
这是我的 HTML:
<div class="internships">
<input class="search" placeholder="Search" type="text" />
<button class="sort button">Search</button>
</div>
<div class="internships">
<ul class="descriptions">
<li><a class="item biology bioinformatics pre dentistry medicine nursing pharmacy" href="/apply/internship-manager/school-of-dentistry/oral-pathology-medicine-and-radiology">Oral Pathology and Medicine & Radiology</a></li>
<li><a class="item biology biomedical engineering biotechnology chemistry neuroscience biochemistry" href="/apply/internship-manager/school-of-dentistry/dentistry-medicine-angela-bruzzaniti">Biomedical and Applied Sciences</a></li>
<li><a class="item biology health sciences pre medicine dentistry" href="/apply/internship-manager/school-of-dentistry/dentistry-rachel-menegaz">Biomedical and Applied Sciences</a></li>
<li><a class="item biology biotechnology chemistry health sciences pre dentistry medicine nursing pharmacy" href="/apply/internship-manager/school-of-dentistry/dentistry-richard-gregory">Biomedical and Applied Sciences</a></li>
</ul>
</div>
*注意:这只是一个部分,页面上还有一些列表,但由于我工作的大学使用的框架,它们必须分成几个部分。
这是我的 jQuery:
$('.sort').click(function() {
$('.search').keyup( function() {
//Declare what is typed in the search box as a variable to use
var value = this.value;
$("#dom_element").text(value);
//if the items with class .item contain the class that matches what is typed into the search box, show them, else hide them.
if( $('.item').hasClass) {
('value').show();
}
else {
$('.item').hide();
}
});
});
因此,如果用户在搜索框中键入护理,我希望只显示类中带有护理锚标记的列表项。我已经尝试了几种不同的方法(list.js 就是其中之一),但我找不到任何能够按类排序的方法。如果我需要澄清任何事情,请告诉我,我们将不胜感激。谢谢!
【问题讨论】:
-
您是否正在寻找与
a元素的类完全匹配的内容?另外,您需要按什么对元素进行排序? -
请注意,数据属性可能更适合该列表,而不是类。
-
Rory,我认为它们不需要精确,我需要按与搜索框中键入的内容的相关性对元素进行排序(链接上的类是与实习,我希望学生能够找到与他们的专业相关的申请)
-
太好了,谢谢你们。我将暂时处理您的答案,然后再回复您。非常感谢。