修复javascript数组初始化

时间:2011-06-17 17:35:37

标签: javascript arrays

我正在尝试用另一个数组初始化一个数组。该数组包含用于加载RSS提要阅读器的选项。用户单击选项按钮以选择要读取的RSS类别。根据选择,我将这样的Feed URL和选项数组初始化。

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++ -->

1 个答案:

答案 0 :(得分:2)

var optionsnews = {
    stacked : true,
    horizontal : false,
    title : "News"
  }

// snip...

options=optionsnews.slice(0, optionsnews.length);

optionsnewsObject,而不是Array。没有Object.slice方法,因为对象既没有数字索引也没有固有排序。

所有这些.slice()的重点是什么?你想要完成什么?