0

HTML:-

在我使用过的 body 标签中onload="variable2.init() ; variable1.init();"

JavaScript:-

var variable1 = {
rssUrl: 'http://feeds.feedburner.com/football-italia/pAjS',

init: function() {
    this.getRSS();
},

getRSS: function() {
    jQuery.getFeed({
        url: variable1.rssUrl,
        success: function showFeed(feed) {
            variable1.parseRSS(feed);
        }
    });
},

parseRSS: function(feed) {
    var main = '';
    var posts = '';
    var className = 'even';
    var pst = {};
    for (i = 0; i < feed.items.length; i++) {
        pst = variable1.parsefootballitaliaRSS(feed.items[i]);
        if (className == 'odd') {
            className = 'even';
        }
        else {
            className = 'odd';
        }
        var shorter = pst.story.replace(/<(?:.|\n)*?>/gm, '');
        item_date = new Date(feed.items[i].updated);
        main += '<div id="content1" class="post-main ' + className + '" onclick="mwl.setGroupTarget(\'#screens1\', \'#blog_posts1\', \'ui-show\', \'ui-hide\');mwl.setGroupTarget(\'#blog_posts1\', \'#post' + (i+1) + '\', \'ui-show\', \'ui-hide\');">';
        main += '<b>' + pst.title.trunc(55, true) + '</b><br />' + shorter.trunc(30, true);
        main += '<div class="datetime">' + item_date.getDateTime() + '</div></div>';

        posts += '<div class="post-wrapper ui-hide" id="post' + (i+1) + '">';
        posts += '<div class="post-title"><b>' + pst.title + '</b></div>';
        posts += feed.items[i].description;
        posts += '</div>';
    }

    jQuery('#main_screen1').html(main);
    jQuery('#blog_posts1').html(posts);

},

parsefootballitaliaRSS: function(item) {
    var match = item.description.match('src="([^"]+)"');
    var part = item.description.split('<font size="-1">');
    var arr = {
        title: item.title,
        link: item.link,
        image: match,
        site_title: item.title,
        story: item.description
    };

    return arr;
}
};

var variable2 = {
    weatherRSS: 'http://feeds.feedburner.com/go/ELkW',

    init: function() {
        this.getWeatherRSS();
    },

    getWeatherRSS: function() {
        jQuery.getFeed({
            url: variable2.weatherRSS,
            success: function showFeed(feed) {
                variable2.parseWeather(feed);
            }
        });
    },

    parseWeather: function(feed) {
        var main = '';
        var posts = '';
        var className = 'even';
        var pst = {};
        for (i = 0; i < feed.items.length; i++) {
            pst = variable2.parsegoRSS(feed.items[i]);
            if (className == 'odd') {
                className = 'even';
            }
            else {
                className = 'odd';
            }
            var shorter = pst.story.replace(/<(?:.|\n)*?>/gm, '');
            item_date = new Date(feed.items[i].updated);
            main += '<div id="content2" class="post-main ' + className + '" onclick="mwl.setGroupTarget(\'#screens2\', \'#blog_posts2\', \'ui-show\', \'ui-hide\');mwl.setGroupTarget(\'#blog_posts2\', \'#post' + (i+1) + '\', \'ui-show\', \'ui-hide\');">';
            main += '<b>' + pst.title.trunc(55, true) + '</b><br />' + shorter.trunc(30, true);
            main += '<div class="datetime">' + item_date.getDateTime() + '</div></div>';

            posts += '<div class="post-wrapper ui-hide" id="post' + (i+1) + '">';
            posts += '<div class="post-title"><b>' + pst.title + '</b></div>';
            posts += feed.items[i].description;
            posts += '</div>';
        }

        jQuery('#main_screen2').html(main);
        jQuery('#blog_posts2').html(posts);

    },

    parsegoRSS: function(item) {
        var match = item.description.match('src="([^"]+)"');
        var part = item.description.split('<font size="-1">');
        var arr = {
            title: item.title,
            link: item.link,
            image: match,
            site_title: item.title,
            story: item.description
        };

        return arr;
    }
};

当我运行程序时,它只读取其中一个变量,即 1 或 2。
我怎样才能更正它们以读取这两个变量?

4

2 回答 2

0

用这个。

<script type="text/javascript">
 window.onload = function() {  

  variable1.init(); 
  variable2.init();

 }
</script>
于 2013-05-24T05:13:19.877 回答
0

尝试这个

<body onload="callFunctions()">

JS-

function callFunctions()
{
variable1.init(); 
variable2.init();
}

更新-

还有 其他不同的方法可以在页面加载时调用多个函数

希望它对你有帮助。

于 2013-05-24T05:04:32.727 回答