Home > Article > Web Front-end > How jQuery implements front-end search function
What I bring to you this time is jQueryHow to implement the front-end search function. The front-end search function requires a combination of HTML and jQuery. This article will give you a good analysis.
html Code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>工程轻量化与可靠性技术实验室</title> </head> <body> <div class="content-right"> <input type="text"><input type="submit" value="搜索"> <h3>应用流体学</h3> <ul id="content_news_list"> <li><span>2015-7-8</span><a href="">这里是文章的标题1</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题2</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题3</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题4</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题5</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题6</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题6</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题6</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题6</a></li> <li><span>2015-7-8</span><a href="">这里是文章的标题4</a></li> </ul> </div> </body>
jQuery Code:
<script type="text/javascript"> $(function(){ $("input[type=text]").change(function () { var searchText = $(this).val();//获取输入的搜索内容 var $searchLi = "";//预备对象,用于存储匹配出的li if (searchText != "") { //获取所有匹配的li $searchLi = $("#content_news_list").find('a:contains('+ searchText +')').parent(); //将内容清空 $("#content_news_list").html(""); } //将获取的元素追加到列表中 $("#content_news_list").html($searchLi).clone(); //判断搜索内容是否有效,若无效,输出not find if ($searchLi.length <= 0) { $("#content_news_list").html("<li>not find</li>") } }) $("input[type=submit]").click(function () { $("searchText").change(); }) }) </script>
Retrieve elements in the list by keyword and add them to ul.
$(':contains(text)') gets the element containing the specified character. The string can be text directly contained in the element, or contained in a child element .
This method implements a simple retrieval function by determining whether the obtained element contains the searched characters.
I believe you have mastered the method after reading the above introduction. For more exciting information, please pay attention to other related articles on the php Chinese website!
Related reading:
How to use Vue’s custom instructions to complete a drop-down menu
How can JS click to jump to the logged-in personal mailbox
The above is the detailed content of How jQuery implements front-end search function. For more information, please follow other related articles on the PHP Chinese website!