Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

打开搜索到的页面时,可以高亮关键字,并滚动到关键字所在位置 #91

Merged
merged 3 commits into from
Nov 5, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
highlight searched keywords and scroll to the first one, when open a …
…page from searching-result-list
  • Loading branch information
fofen committed Nov 2, 2017
commit d5eb7cf4f0a392afce55409bb96c1d216272ed70
23 changes: 22 additions & 1 deletion template/theme/source/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var initSearch = function() {
}
return searchTpl
.replace('{{title}}', title)
.replace('{{link}}', link)
.replace('{{link}}', link + '?search=' + keywords)
.replace('{{preview}}', preview)
}
searchWorker.onmessage = function(event) {
Expand Down Expand Up @@ -158,3 +158,24 @@ $(function() {
// init search
initSearch()
})

// get searched keywords from url
var reg = new RegExp("(^|&)search=([^&]*)(&|$)")
var r = window.location.search.substr(1).match(reg)
var keywords = decodeURI(r[2]).split(',')

// highlight searched keywords
var content = document.body.innerHTML
if (keywords != null && keywords.toString().length > 1) {
for (var i = 0; i < keywords.length; i++) {
var keyword = keywords[i]
var wrap = '<span id="searched" class="searched">' + keyword + '</span>' // id="searched" is for scrolling below
var reg = new RegExp(keyword, 'ig')
content = content.replace(reg, wrap)
}
}
document.body.innerHTML = content

// scroll to the first searched keyword
var elmnt = document.getElementById("searched")
elmnt.scrollIntoView()