Jquery Mobile Gesture在android phonegap应用程序中左右扫描问题

时间:2013-01-01 14:27:17

标签: android cordova jquery-mobile swipe-gesture

我正在尝试在android中的jquery mobile / phonegap中实现左/右滑动。我有示例代码,但是当我滑动时,没有任何内容发生。

这是我的javascript代码

$("#listitem").swiperight(function() {
    $.mobile.changePage("#page1");
});

这是正文内容

<div data-role="page" id="home">
    <div data-role="content">
        <p>
        <ul data-role="listview" data-inset="true" data-theme="c">
            <li id="listitem">Swipe Right to view Page 1</li>
        </ul>
        </p>
    </div>
</div>

<div data-role="page" id="page1">
    <div data-role="content">

        <ul data-role="listview" data-inset="true" data-theme="c">
            <li data-role="list-divider">Navigation</li>
            <li><a href="#home">Back to the Home Page</a></li>
        </ul>

        <p>
            Yeah!<br />You Swiped Right to view Page 1
        </p>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

事件处理程序仅绑定到当前选定的元素,并且在代码进行调用时页面上必须存在 。在加载任何其他HTML之前,文档元素在文档头中可用,因此可以安全地将事件附加到那里而无需等待文档准备好。

此外,如jQM docs中所述,您可以像使用其他jQuery事件一样绑定这些事件,使用live()或bind()。

您可以查看以下示例:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Swipe</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
        <script>
            $('#listitem').live("swiperight", function(){
                $.mobile.changePage("#page1");
            });
        </script>
    </head> 
    <body>
        <div data-role="page" id="home">
            <div data-role="content">
                <p>
                    <ul data-role="listview" data-inset="true" data-theme="c">
                        <li id="listitem">Swipe Right to view Page 1</li>
                    </ul>
                </p>
            </div>
        </div>
        <div data-role="page" id="page1">
            <div data-role="content">
                <ul data-role="listview" data-inset="true" data-theme="c">
                    <li data-role="list-divider">Navigation</li>
                    <li><a href="#home">Back to the Home Page</a></li>
                </ul>
                <p>
                    Yeah!<br />You Swiped Right to view Page 1
                </p>
            </div>
        </div>
    </body>
</html>

示例2:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Swipe Example</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
        <script>
            $(document).on("swiperight", "#listitem", function() {
                $.mobile.changePage("#page1");
            });
        </script>
    </head> 
    <body>
        <div data-role="page" id="home">
            <div data-role="content">
                <p>
                    <ul data-role="listview" data-inset="true" data-theme="c">
                        <li id="listitem">Swipe Right to view Page 1</li>
                    </ul>
                </p>
            </div>
        </div>
        <div data-role="page" id="page1">
            <div data-role="content">
                <ul data-role="listview" data-inset="true" data-theme="c">
                    <li data-role="list-divider">Navigation</li>
                    <li><a href="#home">Back to the Home Page</a></li>
                </ul>
                <p>
                    Yeah!<br />You Swiped Right to view Page 1
                </p>
            </div>
        </div>
    </body>
</html>

示例3:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Swipe Example</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.css" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/mobile/1.1.1/jquery.mobile-1.1.1.min.js"></script>
    </head> 
    <body>
        <div data-role="page" id="home">
            <div data-role="content">
                <p>
                    <ul data-role="listview" data-inset="true" data-theme="c">
                        <li id="listitem">Swipe Right to view Page 1</li>
                    </ul>
                </p>
            </div>
        </div>
        <div data-role="page" id="page1">
            <div data-role="content">
                <ul data-role="listview" data-inset="true" data-theme="c">
                    <li data-role="list-divider">Navigation</li>
                    <li><a href="#home">Back to the Home Page</a></li>
                </ul>
                <p>
                    Yeah!<br />You Swiped Right to view Page 1
                </p>
            </div>
        </div>
        <script>
            $("#listitem").swiperight(function() {
                $.mobile.changePage("#page1");
            });
        </script>
    </body>
</html>