本插件作为jquery插件,使用XML作为数据原始数据,最初设计于使用github的jekyll作为博客站点,我的博客我是链接也是使用这个插件
,另外由于本地请求其search.xml文件,如果想本地查看效果,在打开chrome浏览器时,请使用google-chrome --allow-file-access-from-files --disable-web-security
参数
如果你使用jekyll作为静态页面生成器可以如下构造XML
<?xml version="1.0" encoding="UTF-8"?>
<blogs>
{% for post in site.posts %}
<blog>
<title>{{ post.title | xml_escape}}</title>
<content>{{ post.content | xml_escape }}</content>
<url>http://www.site_ur.com{{ post.url }}</url>
<time>{{ post.date | date: "%Y-%m-%d" }}</time>
</blog>
{% endfor %}
</blogs>
如果你是手动构建xml可以这样做
<?xml version="1.0" encoding="UTF-8"?>
<blogs>
<blog>
<title>这篇文章标题</title>
<content>这篇文章的内容</content>
<url>http://wwww.site_url.com/xxxx.html</url>
<time>2014-2-3</time>
</blog>
<blog>
<title>这篇文章标题</title>
<content>这篇文章的内容</content>
<url>http://wwww.site_url.com/xxxx.html</url>
<time>2014-2-3</time>
</blog>
</blogs>
直接载入jquery.site-search.js文件,但是注意使用本插件先要载入jquery文件,载入后我们现在给搜素表单绑定我们的事件,如demo所示
$("#search_form").siteSearch(Setting)
在传入的Setting,必须为对象,如我在demo中
增加的搜索框有且只有一个输入框,并且有提交按钮,如下所示
<form role="search" id="search_form" data-url="http://www.songyuchao.com/search.xml">
<div class="form-group">
<input type="text" placeholder="请输入搜索关键字" >
</div>
<button type="submit" id="search_submit">搜一下</button>
</form>