我正在开发一个jquery移动网页。在其中一个页面中,我想实现一个RSS阅读器。我在网上找到了一个RSS阅读器,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<link rel="stylesheet" href="themes/CustomTheme.min.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile.structure-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<style type='text/css'>
.ui-footer .ui-btn-right {
}
.articleContent > table > tbody > tr > td > font > br {
display: none;
}
.articleContent > table > tbody > tr > td > font > br + div {
display: none;
}
.articleContent * {
font-size: medium !important;
}
</style>
</head>
<body>
<script type='text/javascript'>//<![CDATA[
// ISCPA added search filter, home icon, updated CDN-Hosted links
// forked from sumukh1's "forked: RSS Reader with jQuery Mobile" http://jsdo.it/sumukh1/4Ton
/* configuration */
var maxLength = 20; /* writing HTML */
document.write('<div data-role="page" id="list">' + ' <div data-role="header" data-position="fixed">' + ' <h1><span id="widgetTitle">...</span> ' + ' <span style="font-size: x-small">(HTML)</span></h1>' + ' </div>' + ' <div data-role="content">' + ' <ul data-role="listview" data-filter="true" id="articleList">');
for (var i = 1; i <= maxLength; i++) {
document.write('<li id="list' + i + '"><a href="#article' + i + '" id="link' + i + '"> </a></li>');
}
document.write(' </ul>' + ' </div>' + '</div>');
for (i = 1; i <= maxLength; i++) {
document.write('<div data-role="page" id="article' + i + '">' + ' <div data-role="header" data-position="inline">' + ' <a href="#list" data-role="button" data-icon="home" data-back="true">Home</a>' + ' <h1 id="articleHeader' + i + '"> </h1>' + ' <a href="#" id="openButton' + i + '" data-role="button" data-icon="plus"' + ' class="ui-btn-right" rel="external">Open</a>' + ' </div>' + ' <div data-role="content">' + ' <div id="articleContent' + i + '" class="articleContent"></div>' + ' <div data-role="controlgroup" data-type="horizontal">' + ' <a href="#article' + String(i - 1) + '" data-role="button" data-icon="arrow-l"' + ' data-inline="true" class="prevButton">Prev</a>' + ' <a href="#article' + String(i + 1) + '" data-role="button" data-icon="arrow-r"' + ' data-inline="true" class="nextButton" data-iconpos="right">Next</a>' + ' </div>' + ' </div>' + '</div>');
} /* JSONP */
$(function() {
// getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
/*
getOnlineFeed('http://www.engadget.com/rss.xml');
getOnlineFeed('http://www.fremont.k12.ca.us/site/RSS.aspx?DomainID=1&ModuleInstanceID=4613&PageID=1');
getOnlineFeed('http://news.google.com/news?hl=ja&ned=us&ie=UTF-8&oe=UTF-8&output=atom&topic=h');
getOnlineFeed('http://www.appbank.net/feed');
getOnlineFeed('http://japanese.engadget.com/rss.xml');
getOnlineFeed('http://www.bebit.co.jp/index.xml');
getOnlineFeed('http://www.ntt.com/rss/release.rdf?link_id=ostop_service_rss');
getOnlineFeed('http://feeds.feedburner.com/gapsis');
getOnlineFeed('http://octoba.net/feed');
getOfflineFeed('google_news_jsonp.js');
*/
}); /* functions */
var listEntries = function(json) {
if (!json.responseData.feed.entries) return false;
$('#widgetTitle').text(json.responseData.feed.title);
var articleLength = json.responseData.feed.entries.length;
articleLength = (articleLength > maxLength) ? maxLength : articleLength;
for (var i = 1; i <= articleLength; i++) {
var entry = json.responseData.feed.entries[i - 1];
$('#link' + i).text(entry.title);
$('#articleHeader' + i).text(entry.title);
$('#openButton' + i).attr('href', entry.link);
$('#articleContent' + i).append(entry.content);
}
$('#article1 .prevButton').remove();
$('#article' + articleLength + ' .nextButton').remove();
if (articleLength < maxLength) {
for (i = articleLength + 1; i <= maxLength; i++) {
$('#list' + i).remove();
$('#article' + i).remove();
}
}
};
var getOnlineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', 'http://ajax.googleapis.com/ajax/services/feed/load?callback=listEntries&hl=ja&output=json-in-script&q=' + encodeURIComponent(url) + '&v=1.0&num=' + maxLength);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
var getOfflineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
//]]>
</script>
</body>
</html>
好的,这是一个动态创建页面和所有内容的JavaScript。但是,我已经有了页面(我也有一个面板),我想要做的是在那里集成代码。我看到的页面如下:
<div data-role="page" id="news" data-theme="a">
<div data-role="panel" id="mypanel">
<ul data-role="listview" data-inset="false">
<li data-role="list-divider">Espacio Joven</li>
<li><a href="#news" class="ui-btn-active ui-state-persist">News</a></li>
<li><a href="#agenda" class="ui-btn-active ui-state-persist">Agenda</a></li>
<li><a href="#info" class="ui-btn-active ui-state-persist">Info</a></li>
<li><a href="#activities" class="ui-btn-active ui-state-persist">Activities</a></li>
<li><a href="#alDia" class="ui-btn-active ui-state-persist">Al Dia</a></li>
<li data-role="list-divider">La Noche Es Joven</li>
</ul>
</div><!-- /panel -->
<div data-role="header">
<a href="#mypanel" data-icon="grid" data-iconpos="notext"></a>
<h1>News</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" class="RSSclass">
</ul>
</div> <!-- /content -->
</div>
有人可以帮我配置RSS代码,以便在
中创建帖子<ul data-role="listview" data-inset="true" class="RSSclass">
</ul>
我有吗?我试图调整代码,例如我删除了创建页面的行(我有一个页面,所以我不需要它),而不是document.write我使用追加,以便将相册附加到列表视图中,但仍然我它有很多问题而且看起来不太好。
如果有人想知道,我试图将代码更改为:
$(document).on("pageshow", '#news', function() {
// ISCPA added search filter, home icon, updated CDN-Hosted links
// forked from sumukh1's "forked: RSS Reader with jQuery Mobile" http://jsdo.it/sumukh1/4Ton
/* configuration */
var maxLength = 20; /* writing HTML */
alert("Hello World!");
//Not sure if .listView('refresh'); is needed. Check it!
for (var i = 1; i <= maxLength; i++) {
$(".RSSclass").append('<li id="list' + i + '"><a href="#article' + i + '" id="link' + i + '"> </a></li>');
}
for (i = 1; i <= maxLength; i++) {
document.write('<div data-role="page" id="article' + i + '">' + ' <div data-role="header" data-position="inline">' + ' <a href="#list" data-role="button" data-icon="home" data-back="true">Home</a>' + ' <h1 id="articleHeader' + i + '"> </h1>' + ' <a href="#" id="openButton' + i + '" data-role="button" data-icon="plus"' + ' class="ui-btn-right" rel="external">Open</a>' + ' </div>' + ' <div data-role="content">' + ' <div id="articleContent' + i + '" class="articleContent"></div>' + ' <div data-role="controlgroup" data-type="horizontal">' + ' <a href="#article' + String(i - 1) + '" data-role="button" data-icon="arrow-l"' + ' data-inline="true" class="prevButton">Prev</a>' + ' <a href="#article' + String(i + 1) + '" data-role="button" data-icon="arrow-r"' + ' data-inline="true" class="nextButton" data-iconpos="right">Next</a>' + ' </div>' + ' </div>' + '</div>');
} /* JSONP */
$(function() {
// getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
/*
getOnlineFeed('http://www.engadget.com/rss.xml');
getOnlineFeed('http://www.fremont.k12.ca.us/site/RSS.aspx?DomainID=1&ModuleInstanceID=4613&PageID=1');
getOnlineFeed('http://news.google.com/news?hl=ja&ned=us&ie=UTF-8&oe=UTF-8&output=atom&topic=h');
getOnlineFeed('http://www.appbank.net/feed');
getOnlineFeed('http://japanese.engadget.com/rss.xml');
getOnlineFeed('http://www.bebit.co.jp/index.xml');
getOnlineFeed('http://www.ntt.com/rss/release.rdf?link_id=ostop_service_rss');
getOnlineFeed('http://feeds.feedburner.com/gapsis');
getOnlineFeed('http://octoba.net/feed');
getOfflineFeed('google_news_jsonp.js');
*/
}); /* functions */
var listEntries = function(json) {
if (!json.responseData.feed.entries) return false;
$('#widgetTitle').text(json.responseData.feed.title);
var articleLength = json.responseData.feed.entries.length;
articleLength = (articleLength > maxLength) ? maxLength : articleLength;
for (var i = 1; i <= articleLength; i++) {
var entry = json.responseData.feed.entries[i - 1];
$('#link' + i).text(entry.title);
$('#articleHeader' + i).text(entry.title);
$('#openButton' + i).attr('href', entry.link);
$('#articleContent' + i).append(entry.content);
}
$('#article1 .prevButton').remove();
$('#article' + articleLength + ' .nextButton').remove();
if (articleLength < maxLength) {
for (i = articleLength + 1; i <= maxLength; i++) {
$('#list' + i).remove();
$('#article' + i).remove();
}
}
};
var getOnlineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', 'http://ajax.googleapis.com/ajax/services/feed/load?callback=listEntries&hl=ja&output=json-in-script&q=' + encodeURIComponent(url) + '&v=1.0&num=' + maxLength);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
var getOfflineFeed = function(url) {
var script = document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
//]]>
});
并在我的html文档的头部添加了CSS,但看起来很糟糕......
我也有错误:Uncaught TypeError: undefined is not a function
排成一行:
$(function() {
// getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
getOnlineFeed('http://www4.lehigh.edu/news/rss/LUnews_rss.xml');
有什么想法吗?
答案 0 :(得分:1)
您必须修改listEntries
函数,因为这是JSOP在回调时调用的函数。
var listEntries = function (json) {
if (!json.responseData.feed.entries) return false;
$('#widgetTitle').text(json.responseData.feed.title);
var articleLength = json.responseData.feed.entries.length;
articleLength = (articleLength > maxLength) ? maxLength : articleLength;
for (var i = 1; i <= articleLength; i++) {
var entry = json.responseData.feed.entries[i - 1];
$(".RSSclass").append('<li id="list' + i + '"><a href="#article' + i + '" id="link' + i + '">' + entry.title + '</a></li>');
}
$('.RSSclass').listview('refresh');
};
此外,您必须将自定义页面设置添加到页面正文中,如下所示:
<div data-role="page" id="news" data-theme="a">
<div data-role="panel" id="mypanel">
<ul data-role="listview" data-inset="false">
<li data-role="list-divider">Espacio Joven</li>
<li><a href="#news" class="ui-btn-active ui-state-persist">News</a>
</li>
<li><a href="#agenda" class="ui-btn-active ui-state-persist">Agenda</a>
</li>
<li><a href="#info" class="ui-btn-active ui-state-persist">Info</a>
</li>
<li><a href="#activities" class="ui-btn-active ui-state-persist">Activities</a>
</li>
<li><a href="#alDia" class="ui-btn-active ui-state-persist">Al Dia</a>
</li>
<li data-role="list-divider">La Noche Es Joven</li>
</ul>
</div>
<!-- /panel -->
<div data-role="header"> <a href="#mypanel" data-icon="grid" data-iconpos="notext"></a>
<h1>News</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-inset="true" class="RSSclass"></ul>
</div>
<!-- /content -->
</div>
然后您必须使用document.write
这是一个有效的JsBin。 http://jsbin.com/APojAlu/1/edit?html,output 希望有所帮助。
答案 1 :(得分:0)
我复制了您的代码并有两条错误消息
如果您在声明$(document).on("pageshow", '#news', function() {
之前添加以下代码,则会显示这些代码并显示Feed中的内容:
<script type='text/javascript'>//<![CDATA[
var maxLength = 20; /* writing HTML */
function listEntries (json) {
if (!json.responseData.feed.entries) return false;
$('#widgetTitle').text(json.responseData.feed.title);
var articleLength = json.responseData.feed.entries.length;
articleLength = (articleLength > maxLength) ? maxLength : articleLength;
for (var i = 1; i <= articleLength; i++) {
var entry = json.responseData.feed.entries[i - 1];
$('#link' + i).text(entry.title);
$('#articleHeader' + i).text(entry.title);
$('#openButton' + i).attr('href', entry.link);
$('#articleContent' + i).append(entry.content);
}
$('#article1 .prevButton').remove();
$('#article' + articleLength + ' .nextButton').remove();
if (articleLength < maxLength) {
for (i = articleLength + 1; i <= maxLength; i++) {
$('#list' + i).remove();
$('#article' + i).remove();
}
}
};
$(document).on("pageshow", '#news', function() {
// rest follows here
这应该让你去。