0
我想用另一個數組初始化一個數組。該數組包含用於加載RSS提要閱讀器的選項。用戶點擊一個選項按鈕來選擇要讀取的RSS類別。基於選擇,我會像這樣初始化feed URL和選項數組。修復javascript數組初始化
feeds = feedsnews.slice(0, feedsnews.length);
options = optionsnews.slice(0, optionsnews.length);
(整個JavaScript是在文本的末尾)。 然後我將這些數組發送到加載新聞閱讀器的代碼。這似乎只初始化數組中的第一個元素。
用所有元素初始化數組的方法是什麼?數組是否以正確的方式聲明瞭RSS加載器來獲取它們?
下面是代碼:
<script language="JavaScript">
var feedsnews = [ {title: 'Reuters Top News', url: 'http://feeds.reuters.com/reuters/topNews' }, {title: 'Reuters International',
url: 'http://feeds.reuters.com/reuters/worldNews' }, {title: 'Reuters US News', url: 'http://feeds.reuters.com/Reuters/domesticNews' }];
var optionsnews = {
stacked : true,
horizontal : false,
title : "News"
}
var feedscat = [ {title: 'Catholic News Agency', url: 'http://www.catholicnewsagency.com/rss/news.xml' }, {title: 'Zenit - English', url: 'http://feeds.feedburner.com/zenit/english' }, {title: 'Zenit - Français', url:
'http://feeds.feedburner.com/zenit/french' }];
var optionscat = {
stacked : true,
horizontal : false,
title : "Catholic"
}
</script>
<SCRIPT LANGUAGE="JavaScript">
var feeds=feedsnews.slice();
var options=optionsnews.slice();
function GetSelectedItem() {
feeds=feedsnews.slice(0, feedsnews.length);
options=optionsnews.slice(0, optionsnews.length);
chosen = "";
len = document.f1.r1.length;
for (i = 0; i <len; i++) {
if (document.f1.r1[i].checked) {
chosen = document.f1.r1[i].value
}
}
if (chosen == "") {
alert("No Location Chosen")
}
else if (chosen =="News") {
feeds = feedsnews.slice(0,feedsnews.length);
options = optionsnews.slice(0,optionsnews.length);
}
else if (chosen =="Catholic") {
feeds = feedscat.slice(0,feedscat.length);
options = optionscat.slice(optionscat.length);
}
else if (chosen =="Community") {
}
else if (chosen =="Personal") {
}
else if (chosen =="Professional") {
}
else {
alert(chosen);
}
$("#snews").load("loadnews.php");
}
</script>
HTML (for #snews div)
<div id="snews" style="position:absolute; top:30px; right: 30px; width: 430px; height: 380px; overflow-y: auto; overflow-x: hidden; background: white;">
<?php require("loadnews.php"); ?>
</div> <!-- End snews -->
PHP (loadnews.php)
<!-- ++Begin Dynamic Feed Wizard Generated Code++ -->
<!--
// Created with a Google AJAX Search and Feed Wizard
// http://code.google.com/apis/ajaxsearch/wizards.html
-->
<!--
// The Following div element will end up holding the actual feed control.
// You can place this anywhere on your page.
-->
<div id="feed-control">
<span style="color:#676767;font-size:11px;margin:10px;padding:4px;">Loading...</span>
</div>
<script type="text/javascript">
function LoadDynamicFeedControl() {
new GFdynamicFeedControl(feeds, 'feed-control', options);
}
// Load the feeds API and set the onload callback.
google.load('feeds', '1');
google.setOnLoadCallback(LoadDynamicFeedControl);
</script>
<!-- ++End Dynamic Feed Control Wizard Generated Code++ -->
正如我所說我想將數組複製到數組中,以便將對象選項轉換爲對象。然後我將副本發送到一個函數,根據用戶選擇顯示RSS提要。 – Martine
複製的要點是什麼?爲什麼要複製?爲什麼不直接傳遞'feedsnews'和'optionsnews'? JavaScript中的通用對象圖形複製/克隆不容易或簡單。 –
複製,因爲飼料數組將取決於用戶選擇。我有一個feeds數組和一個options對象作爲變量來將選擇傳遞給一個函數。 – Martine