0

我正在使用以下代码(位于文件 swipe.js 中):

$(document).ready(function() {

    $('.ui-slider-handle').live('touchstart', function(){
        // When user touches the slider handle, temporarily unbind the page turn handlers
        doUnbind();
    });

$('.ui-slider-handle').live('mousedown', function(){
    // When user touches the slider handle, temporarily unbind the page turn handlers
    doUnbind();
    });

$('.ui-slider-handle').live('touchend', function(){
    //When the user let's go of the handle, rebind the controls for page turn
    // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered
    setTimeout( function() {doBind();}, 100 );
});

$('.ui-slider-handle').live('mouseup', function(){
    //When the user let's go of the handle, rebind the controls for page turn
    // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered
    setTimeout( function() {doBind();}, 100 );
});

// Set the initial window (assuming it will always be #1
window.now = 1;

//get an Array of all of the pages and count
windowMax = $('div[data-role="page"]').length;

doBind();
});
// Functions for binding swipe events to named handlers
function doBind() {
    $('div[data-role="page"]').live("swipeleft", turnPage);
    $('div[data-role="page"]').live("swiperight", turnPageBack);
    }

function doUnbind() {
    $('div[data-role="page"]').die("swipeleft", turnPage);
    $('div[data-role="page"]').die("swiperight", turnPageBack);
    }

// Named handlers for binding page turn controls
function turnPage(){
    // Check to see if we are already at the highest numbers page
    if (window.now < windowMax) {
    window.now++
    $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings")}, false, true);
}
else
            {
                window.now= window.now-2;
                $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings")}, false, true);
}
}

function turnPageBack(){
    // Check to see if we are already at the lowest numbered page
    if (window.now != 1) {
    window.now--;
    $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings"), reverse: "true"}, true, true);
}
else
            {
                window.now = window.now+2;
                $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings"), reverse: "true"}, true, true);
}
}

我还有另一个名为 loadFeed.js 的文件。它们都按以下顺序加载到 index.html 中:

<script type="text/javascript" src="script/swipe.js"></script>
<script type="text/javascript" src="script/loadFeed.js"></script>

这是 loadFeed.js 文件:

google.load("feeds", "1");
alert(window.now);
switch(now)
{
case 1:
    var link = "http://www.zive.sk/rss/sc-47/default.aspx";
    var listviewID = "feedZive";
    break;
case 2:
    var link = "http://mobilmania.azet.sk/rss/sc-47/default.aspx";
    var listviewID = "feedMobil";
    break;
case 3: 
    var link = "http://www.automoto.sk/rss";
    var listviewID = "feedAuto";
    break;
}
alert(link);
alert(listviewID);
alert("#"+listviewID);

function initialize() {
    var feed = new google.feeds.Feed(link);
    feed.setNumEntries(window.localStorage.getItem("entriesNumber"));
    feed.load(function(result) {
        if (!result.error) {
            var feedlist = document.getElementById(listviewID);
            for (var i = 0; i < result.feed.entries.length; i++) {
                var li = document.createElement("li");
                var entry = result.feed.entries[i];
                var A = document.createElement("A");
                A.setAttribute("href",entry.link);
                A.appendChild(document.createTextNode(entry.title));
                li.appendChild(A);
                feedlist.appendChild(li);
            }
            $("#"+listviewID).listview("refresh");
        }
    });
}
google.setOnLoadCallback(initialize);

有没有办法在 loadFeed.js 中访问 window.now 变量?

4

1 回答 1

0

是的。该window对象随处可用。它是全局范围变量的麦克爸爸。您可以window.now在“loadFeed.js”或任何 javascript 块或文件中使用。

在您的情况下,您将now属性添加到window文档就绪回调中的对象。所以这不会立即发生。您不应该期望 window.now 立即具有价值。我会建议:

1)将代码包装在“loadFeed.js”中,该代码在window.now文档就绪回调中访问,就像大部分 swipe.js 代码一样:

    $(document).ready(function() {
        //your code
    });

或者

2) 在 swipe.js中,window.now在文档就绪回调之外给出一个值。

于 2012-09-12T14:29:57.990 回答