我有一個常見問題頁面,我想包含搜索功能,以便人們不必閱讀整個頁面以找到他們正在尋找的內容。使用JavaScript在頁面上搜索文字
jQuery("#search_submit").on("click", function() {
//var search_str = jQuery("#search_text").val();console.log(search_str);
findString('');
return false;
});
var TRange=null;
function findString (str) {
str = document.getElementById('search_text').value;//jQuery("#search_text").val();
if (parseInt(navigator.appVersion)<4) return;
var strFound;
if (window.find) {
// CODE FOR BROWSERS THAT SUPPORT window.find
var original_content = window;
strFound=original_content.find(str);
if (!strFound) {
strFound=original_content.find(str,0,1);
while (original_content.find(str,0,1)) continue;
}
}
else if (navigator.appName.indexOf("Microsoft")!=-1) {
// EXPLORER-SPECIFIC CODE
if (TRange!=null) {
TRange.collapse(false);
strFound=TRange.findText(str);
if (strFound) TRange.select();
}
if (TRange==null || strFound==0) {
TRange=self.document.body.createTextRange();
strFound=TRange.findText(str);
if (strFound) TRange.select();
}
}
else if (navigator.appName=="Opera") {
alert ("Opera browsers not supported, sorry...")
return;
}
if (!strFound) alert ("String '"+str+"' not found!")
return;
}
我使用這個script但我不知道我在做什麼錯。它只搜索搜索文本輸入字段之前的部分頁面。
這裏是的jsfiddle http://jsfiddle.net/sandyrig/h86knpsq/1/
一切設置嘗試搜索詞「東西」或「笑」,你就會明白我的意思。
編輯
而且,居然在頁面中的所有問題都將處於摺疊狀態時,頁面加載,從而使發現,即使在摺疊答覆文件我會定製腳本,當達到同時uncollapse它搜索整個頁面。
您可以讓用戶意識到所有*主要*瀏覽器都包含此功能,似乎有點不切實際,不是嗎? – Script47
你正在使用的腳本似乎非常非常老。您鏈接到參考Netscape&IE4的那一頁:) – maxedison
@ Script47它是客戶和設計的一部分,我們的目標受衆不是很懂電腦的人。 – Atif