我以前没有在这里发过帖子,但我在发帖前尝试做一些研究,以免浪费任何人的时间,但我没有发现有人发过类似的问题。
我在一个页面上获取两个 RSS 提要时遇到了这个问题,我编辑了http://www.w3schools.com/php/php_ajax_rss_reader.asp示例。基本上当我从 html onload="runscripts();" 函数:showRSS('Google'); rssevents('事件'); 只有其中一个填充 DIV。我确实让它在我身边工作,但偶尔一个提要会填充另一个 DIV,即使变量完全不同。
本质上,我将代码加倍并创建了不同的变量,以便希望我的单个页面上有两个 rss 提要?但它不起作用。
欢迎任何帮助:) 甚至为我指明正确的方向!干杯:)
继承人的代码:
body on load 调用两个函数:
function runscripts(){
showRSS('Google');
rssevents('events');
}
Javascript 标头:
function showRSS(str)
{
if (str.length==0)
{
document.getElementById("rssOutput").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(str=='Google')
{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrss.php?q="+str,true);
xmlhttp.send();
}
else{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("upcomingevents").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrssEvents.php?q="+str,true);
xmlhttp.send();
}
}
function rssevents(str)
{
if (str.length==0)
{
document.getElementById("upcomingevents").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("upcomingevents").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getrssEvents.php?q="+str,true);
xmlhttp.send();
}
php代码:
getrss.php:
<?php
//get the q parameter from URL
$q=$_GET["q"];
//find out which feed was selected
if($q=="Google")
{
$xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
}
elseif($q=="MSNBC")
{
$xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");
}
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(1)->childNodes->item(1)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(1)->childNodes->item(1)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(1)->childNodes->item(1)->nodeValue;
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
$k=4;
$findme = '<img src';
for ($i=1; $i<=$k; $i++)
{
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
$pos = strpos($item_desc,$findme);
if($pos==false)
{
$k=$k+1;
}
else{
echo ("<p><div class='rsstitle' style='height:120px;'><div class='rsstable2'>" . $item_desc);
echo ("</div><div class='rsstable'><a href='" . $item_link . "' target='_blank'>" . $item_title . "</a></div></div><p/>");
}
}
?>
getrssEvents.php:
<?php
//get the q parameter from URL
$q=$_GET["q"];
//find out which feed was selected
if($q=="Google")
{
$xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
}
elseif($q=="events")
{
$xml=("http://groupspaces.com/LoughboroughFinanceSociety/api/events?alt=rss");
}
$xmlDoc2 = new DOMDocument();
$xmlDoc2->load($xml);
//get elements from "<channel>"
$channel=$xmlDoc2->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('summary')
->item(0)->childNodes->item(0)->nodeValue;
//$channel_desc = $channel->getElementsByTagName('gs:location')
//->item(0)->childNodes->item(0)->nodeValue;
//get and output "<item>" elements
$x=$xmlDoc2->getElementsByTagName('item');
for ($i=0; $i<=2; $i++)
{
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('summary')
->item(0)->childNodes->item(0)->nodeValue;
//$item_desc=$x->item($i)->getElementsByTagName('gs')
///->item(0)->childNodes->item(0)->nodeValue;
//echo ("<div class='rsstitle'>" . $item_desc);
//echo ("<a href='" . $item_link . "'>" . $item_title . "</a></div><p/>");
echo(" <div class='rssevents'><div class='rsseventtitle'> ".$item_title."</div>". $item_link."</div>");
}
?>
为任何帮助而欢呼。谢谢