在使用 JavaScript 时,我遇到了连接空字符串的问题。例如,声明
var usersToAppend, leadsToAppend = "";
$(document).ready(function() {
console.log(usersToAppend);
parser = new DOMParser();
//Get list of users
var usersXML = parser.parseFromString('<%=WebConnector.getSomeXML()%>', "text/xml");
console.log(usersXML);
var users = usersXML.getElementsByTagName("users")[0].childNodes;
for(var k = 0; k < users.length; k++)
usersToAppend += users[k].childNodes[0].nodeValue + " -- "+users[k].attributes.getNamedItem("email").nodeValue+"<br>";
});
导致以usersToAppend
开头undefined
。我该如何避免这个问题?
编辑:我不正确地初始化usersToAppend
和leadsToAppend
. 正确的初始化代码是
var usersToAppend = "", var leadsToAppend = "";