簡體   English   中英

JavaScript自定義搜索功能無效

[英]JavaScript custom search function not working in IE

嗨,我正在嘗試為json數組創建自定義搜索

我的代碼在chrome和firfox中運行良好,但在ie中沒有。

代碼的工作原理如下。 它遍歷每個搜索詞“ needle”,如果在問題中找到該詞,則會將其添加到數組中。 如果它在同一問題中找到其他搜索詞,則命中計數器將遞增。

所有代碼都在這里,可以運行。

我得到的錯誤是:

Line:24
Char:3
Error:qna_questions[..].q is null or not an object

qna_questions = [
{ 'q':'this is a very good question','id':'1'},
{ 'q':'i like pllo in the summer','id':'2'},
{ 'q':'it rains allot in the summer mushroom','id':'3'},
{ 'q':'i love people and straberry cake','id':'4'},
{ 'q':'i love people and berry rain','id':'5'},
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'},
];

var search_result = new Array();
var needle = "dog fat summer allot mushroom";
var needle = needle.split(' ');
for(j = 0; j < qna_questions.length; j++) {

    for(i =0; i < needle.length; i++) {

        if(qna_questions[j].q.search(needle[i].toLowerCase()) != -1 ) {

            if( search_result[qna_questions[j].id] === undefined ) {
                search_result[qna_questions[j].id] = { 
                        'q':qna_questions[j].q,
                        'id':qna_questions[j].id,
                        'hits':0
                        };
            } else {
                search_result[qna_questions[j].id].hits++;


            }

        }
    }
}
search_result.sort(function(a,b) { return parseInt(b.hits) - parseInt(a.hits) } );
for (x in search_result)
  {
  alert(search_result[x].q + ": "+search_result[x].hits +"<br />");
  }
qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'}, 
]; 

注意qna_questions結尾處的額外逗號...在IE中將在數組末尾再一個空條目....刪除逗號,你應該是好的。

qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'},
{ 'q':'the dog is fat','id':'7'}
]; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM