2013-03-12 57 views
0

如何從我的內容中轉義$?不斷得到相同的錯誤。請幫忙。

類型錯誤: '未定義' 不是一個對象(評價 '×[I] .getElementsByTagName( 「4號線」)[0] .childNodes [0] .nodeValue')

// Populate the database 
// 
function populateDB(tx) { 
    alert("Populate!"); 
    tx.executeSql('DROP TABLE IF EXISTS news'); 
    tx.executeSql('CREATE TABLE IF NOT EXISTS news (id, title, line1, line2 TEXT, line3 TEXT, line4 TEXT, line5 TEXT)'); 

    if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else{// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET","news.xml",false); 
    xmlhttp.send(); 
    xmlDoc=xmlhttp.responseXML; 


    var x=xmlDoc.getElementsByTagName("news"); 
    for (i=0;i<x.length;i++) 
    { 
     p_id = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue; 
     p_title = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
     p_line1 = x[i].getElementsByTagName("line1")[0].childNodes[0].nodeValue; 
     p_line2 = x[i].getElementsByTagName("line2")[0].childNodes[0].nodeValue; 
     p_line3 = x[i].getElementsByTagName("line3")[0].childNodes[0].nodeValue; 
     p_line4 = x[i].getElementsByTagName("line4")[0].childNodes[0].nodeValue; 
     p_line5 = x[i].getElementsByTagName("line5")[0].childNodes[0].nodeValue; 

     tx.executeSql('INSERT INTO news (id,title,line1,line2,line3,line4,line5) VALUES (?,?,?,?,?,?,?)',[p_id,p_title,p_line1,p_line2,p_line3,p_line4,p_line5]); 

    } 


} 
+0

如果你這樣列出你的for循環,那麼你需要你的評估輸入。 empty(x [i] .getElementsByTagName(「title」)[0] .childNodes [0] .nodeValue;)continue; – 2013-03-12 03:29:21

+0

@Marko ReferenceError:找不到變量:空 – NOOPA 2013-03-12 03:34:16

+0

這意味着你必須應用它最有可能的childNodes [0] .nodeValue或節點值 – 2013-03-12 03:40:32

回答

0

可以是你應該嘗試。

if((y = x[i].getElementsByTagName("line4")[0].childNodes[0]) == '[object Text]')   
    p_line4 =y.nodeValue; 
else 
    p_line4 = null; //or assign empty string 

編輯。

+0

存在語法錯誤。 – NOOPA 2013-03-12 04:42:04

+0

有一個額外的括號。 – 2013-03-12 05:22:21

相關問題