有人能看到这段代码有问题吗?我的应用程序有效,直到我添加这些额外的行:
var descriptionNode = item.getElementsByTagName("description")[0];
var description = descriptionNode.firstChild.data;
var item = { ...
...
...
...
...
description: description,
...
};
和查看页面
var descriptionNode = document.createElement("td");
descriptionNode.setAttribute("class", "description");
descriptionNode.appendChild(document.createTextNode(item["description"]));
tr.appendChild(descriptionNode);
...
<th scope="col">Description</th>
和rss.xml文件
<description>This is a description</description>
完整摘录:
<html>
<head>
<script type="text/javascript">
var items = []; // contains newest items
var unsorted = []; // contains all items
function init() {
if (!localStorage.updateInterval) {
localStorage.updateInterval = 5;
}
if (!localStorage.types) {
localStorage.types = "audio:true;art:true;literature:true;";
}
chrome.browserAction.setBadgeBackgroundColor({color:[255, 102, 0, 255]});
setTimeout(makeRequest, 3000);
}
function makeRequest() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
parseItems(xmlhttp.responseXML);
}
};
xmlhttp.open("GET", "http://url.com/rss.xml", true);
xmlhttp.send(null);
setTimeout(makeRequest, 1000 * 60 * localStorage.updateInterval);
}
function parseItems(data) {
var items = data.getElementsByTagName("item");
for (var i = 0; i < items.length; i++) {
var item = items[i];
var dateNode = item.getElementsByTagName("pubDate")[0];
var date = formatDate(dateNode.firstChild.data);
var titleNode = item.getElementsByTagName("title")[0];
var title = formatTitle(titleNode.firstChild.data);
var type = titleNode.firstChild.data.split("[")[1].split("]")[0];
var linkNode = item.getElementsByTagName("link")[0];
var link = trimSpaces(linkNode.firstChild.data);
var authorNode = item.getElementsByTagName("author")[0];
var author = authorNode.firstChild.data;
var descriptionNode = item.getElementsByTagName("description")[0];
var description = descriptionNode.firstChild.data;
var item = { date: date,
title: title,
featured: false,
type: type,
author: author,
description: description,
link: link };
unsorted[i] = items;
}
processItems();
}
...
视图
<script type="text/javascript">
var items = [];
var background;
function init() {
background = chrome.extension.getBackgroundPage();
items = background.items;
createItemTable();
}
function createitemTable() {
var table = document.getElementById("item-table");
var tbody = document.createElement("tbody");
for (x in items) {
var item = items[x];
var tr = document.createElement("tr");
if (item["featured"]) {
tr.setAttribute("class", "featured");
item["featured"] = false;
}
var dateNode = document.createElement("td");
dateNode.setAttribute("class", "date");
dateNode.appendChild(document.createTextNode(item["date"]));
tr.appendChild(dateNode);
var titleNode = document.createElement("td");
titleNode.setAttribute("class", "title");
titleNode.appendChild(document.createTextNode(item["title"]));
tr.appendChild(titleNode);
var typeNode = document.createElement("td");
typeNode.setAttribute("class", "type");
typeNode.appendChild(document.createTextNode(item["type"]));
tr.appendChild(typeNode);
var authorNode = document.createElement("td");
authorNode.setAttribute("class", "author");
authorNode.appendChild(document.createTextNode(item["author"]));
tr.appendChild(authorNode);
var descriptionNode = document.createElement("td");
descriptionNode.setAttribute("class", "description");
descriptionNode.appendChild(document.createTextNode(item["description"]));
tr.appendChild(descriptionNode);
tbody.appendChild(tr);
}
table.appendChild(tbody);
}
function openTab(url) {
chrome.tabs.create({url: url});
}
</script>
</head>
<body onload="init();" onunload="background.updateBadge();">
<img src="img/logo_popup.png" id="logo" alt="" title="" onclick="openTab('http://www.site.com/');" />
<table id="item-table">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Title</th>
<th scope="col">Type</th>
<th scope="col">Author</th>
<th scope="col">Description</th>
</tr>
</thead>
</table>
</body>
Rss.xml
<rss version="2.0">
<channel>
<title>Site TItle</title>
<link>http://www.site.com</link>
<language>en-us</language>
<pubDate>Sun, 03 Jul 2011 14:40:50 +0000</pubDate>
<lastBuildDate>Sun, 03 Jul 2011 14:40:50 +0000</lastBuildDate>
<copyright/>
<item>
<title>
[art] - This is a title
</title>
<link>http://www.site.com/id?=333</link>
<author>Author</author>
<description>This is a description</description>
<pubDate>Sun, 03 Jul 2011 12:38:12 +0000</pubDate>
</item>
<item>
<title>
[music] - This is a title
</title>
<link>http://www.site.com/id?=332</link>
<author>Author</author>
<description>This is a description</description>
<pubDate>Sun, 03 Jul 2011 12:14:53 +0000</pubDate>
</item>
</channel>
</rss>
答案 0 :(得分:1)
发现问题:使用php创建xml文件时,其中一个条目的数据库字段description
为空,导致标记输出如<description />
。将其修复为<description></description>
可以解决问题。