如何加载多个ul并提取特定的li?
我有一个文档(站点地图),其中有一些ul
。
<h3>Weekday</h3>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li>
</ul>
<h3>Car</h3>
<ul>
<li>green BMW</li>
<li>Mercedes</li>
<li>Audi red</li>
<li>Renault</li>
<li>VW orange</li>
</ul>
<h3>Color</h3>
<ul>
<li>green on Saturday</li>
<li>blue</li>
<li>red</li>
<li>orange Friday</li>
</ul>
我正在尝试创建一种自定义 AJAX 搜索。搜索不是基于真实的搜索结果,而是基于我的站点地图中的所有值。
输入时,我将站点地图的 #inner
div
加载到当前页面中。这实际上很好用。然而现在我的结果只是 ul ,其中显示了找到的列表元素,并将不匹配的元素设置为显示:无。
这是我当前的 jQuery 代码:
var $sr = $('#searchresults'); //container where i want my list to go
$sr.load("/sitemap/" + " #inner", function() {
$('#searchresults h3').remove(); //removing h3's
$('#searchresults ul li').hide(); //hide all results at beginning
var term = $('.s').val(); //current searchterm in inputbox
var found = $('#searchresults ul li:icontains("' + $('.s').val() + '")'); //check for inputvalue
found.addClass('matched'); //if matched addClass of .matched
$('#searchresults .matched').show();
$('#searchresults').children().slice(10).hide(); //max number of results
}); // end keydown
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Weekday</h3>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li>
</ul>
<h3>Car</h3>
<ul>
<li>green BMW</li>
<li>Mercedes</li>
<li>Audi red</li>
<li>Renault</li>
<li>VW orange</li>
</ul>
<h3>Color</h3>
<ul>
<li>green on Saturday</li>
<li>blue</li>
<li>red</li>
<li>orange Friday</li>
</ul>
它工作得非常好,但我想知道是否以及如何不仅可以加载站点地图并将不匹配的值设置为 display:none
,而是创建一个包含所有结果的新列表。
所以实际上我想从其父 ul
中提取所有 .matched 元素并将它们包装到一个新的 ul
中。所以我只在 ul 上包含所有 .matched 结果,而不是在 ul 上包含一些 .matched
元素。
我怎样才能做到这一点?
I have a document (sitemap) with a few ul
s in it.
<h3>Weekday</h3>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li>
</ul>
<h3>Car</h3>
<ul>
<li>green BMW</li>
<li>Mercedes</li>
<li>Audi red</li>
<li>Renault</li>
<li>VW orange</li>
</ul>
<h3>Color</h3>
<ul>
<li>green on Saturday</li>
<li>blue</li>
<li>red</li>
<li>orange Friday</li>
</ul>
I'm trying to create a kind of custom AJAX search. The search is not based on real search-results but rather on all values in my sitemap.
When typing I'm loading the #inner
div
of my sitemap into my current page. This works actually fine. However right now my results are just the ul
s with the found list-elements displayed and the not matched ones set to display: none.
This is my current jQuery code:
var $sr = $('#searchresults'); //container where i want my list to go
$sr.load("/sitemap/" + " #inner", function() {
$('#searchresults h3').remove(); //removing h3's
$('#searchresults ul li').hide(); //hide all results at beginning
var term = $('.s').val(); //current searchterm in inputbox
var found = $('#searchresults ul li:icontains("' + $('.s').val() + '")'); //check for inputvalue
found.addClass('matched'); //if matched addClass of .matched
$('#searchresults .matched').show();
$('#searchresults').children().slice(10).hide(); //max number of results
}); // end keydown
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Weekday</h3>
<ul>
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
<li>Saturday</li>
<li>Sunday</li>
</ul>
<h3>Car</h3>
<ul>
<li>green BMW</li>
<li>Mercedes</li>
<li>Audi red</li>
<li>Renault</li>
<li>VW orange</li>
</ul>
<h3>Color</h3>
<ul>
<li>green on Saturday</li>
<li>blue</li>
<li>red</li>
<li>orange Friday</li>
</ul>
It works really well however I wonder if and how it is possible to not just load the sitemap and set the not-matched values to display:none
but rather create a new list with all the results.
So actually I want to extract all .matched elements from their parent ul
s and wrap them in a new ul
. So I have just on ul with all the .matched results and not a few ul's with some .matched
elements.
How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以你想要类似的东西
,假设你是所有 li 的类..但是如果你想要通过 html 匹配你可以做..
更短更容易阅读..只是一些替代品..干杯-杰里米
So you want something like
that is asumed that you what all li 's with class .. but if you want by the html match you could do..
little shorter and easier to read.. just some alts there.. hth Cheers -Jeremy
说实话,如果你用一些 id 标签包裹这两个地方,那会很快,这样
你就可以做到了
,就这样了..你现在真的在做......那么为什么要添加更多呢?如果是我,我会用简单的方法去做,然后继续前进。那是 my2cents.. 干杯 -Jeremy
to be honest it'd be fast if you wrap both places with some id'd tag so
than you can just do
and that would be it.. you'r doing it now really.. so why add more to it? If it was me I'd do it the simple way and move on. that is my2cents.. hth Cheers -Jeremy