【问题标题】:My .on Swipe only works once before it breaks and have to refresh everything - JQuery Mobile / Javascript我的 .on Swipe 在中断之前只能工作一次并且必须刷新所有内容 - JQuery Mobile / Javascript
【发布时间】:2019-04-10 00:10:52
【问题描述】:

所以当我点击我的图片时,我设法为我编写了一个脚本,它会显示一个弹出窗口,当我在图片上向左或向右滑动时,它会在我的 javascript 中显示隐藏的图片。但是,当我到达我的索引页面并转到房屋 html 时。当我单击照片时,在我必须进入我的 javascript 文件之前,滑动功能不起作用,并且基本上在它再次工作之前重写滑动功能,但是在我回到我的索引页面后会中断。

这是我网站的索引页:http://titan.dcs.bbk.ac.uk/~aaldib01/mad/madfma/index.html

然后是房子 > 2 卧室露台 > house1.html

有没有办法让我解决问题或改进我的 javascript 以使其不再成为问题?

谢谢。

*请注意我认为的问题在于图像代码的放置位置。 (我已经删除了大部分其他代码,因为这不会影响它)

我尝试过使用 .bind("swiperight", function() 但它给了我相同的结果。它工作一次然后在我转到 index.html 后就不能工作了 >> house1.html

这是 house1.html 代码(data-role="content":


<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=yes">
    <link rel="stylesheet" href="css/themes/blue.min.css" />
    <link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <script src="images.js"></script>
    <title>House 1</title>
</head>
<body>
    <div data-role="page" id="house1" data-theme="a" data-dom-cache="true">

        <div data-role="content">
            <a href="#popupImg" data-rel="popup" data-position-to="window" data-transition="pop">
                <img src="pictures/houses/house1/image1.PNG"/ style="width: 50%;"/>
                <div data-role="popup" id="popupImg" data-theme="a" class="ui-corner-all">
                <a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>
                <img src="pictures/houses/house1/image1.PNG" style="width: 100%;" />
            </a>
                </div>

        <div data-role="footer" data-position="fixed">
            <div data-role="navbar" data-id="footernav">
                <ul>
                    <li><a href="about.html">About</a></li>
                    <li><a href="">Favourites</a></li>
                    <li><a href="contact.html">Contact</a></li>
                </ul>
            </div>
        </div>
    </div> 
</body>
</html>

这是 javascript 文件:


$(document).ready(function () {
    var i = 0;
    var imgURL = [];

    imgURL.push('pictures/houses/house1/image1.PNG');
    imgURL.push('pictures/houses/house1/image2.PNG');
    imgURL.push('pictures/houses/house1/image3.PNG');
    imgURL.push('pictures/houses/house1/image4.PNG');

    $("#house1").on("swiperight",function () {
        if (i < (imgURL.length - 1)) {
            i++
        } else {
            i = 0;
        }
        var imgStr = "<img src=" + imgURL[i] + " style='width:100%'>";
        $('#popupImg').html(imgStr);
    });

    $("#house1").on("swipeleft",function () {
        if (i > 0) {
            i--
        } else {
            i = (imgURL.length - 1);
        }
        var imgStr = "<img src=" + imgURL[i] + " style='width:100%'>";
        $('#popupImg').html(imgStr);
    });


});

【问题讨论】:

    标签: javascript jquery-mobile


    【解决方案1】:

    问题是,如果您从索引页面开始并导航到 House 1,则不会加载您的 image.js 脚本。您可以在直接加载 House 1 时通过查看您网站的源来检查这一点(它会在那里)而不是从索引开始并浏览到 House 1(它会从 &lt;head&gt; 中丢失)。

    这是因为 jQuery Mobile 使用 AJAX 在页面之间导航。默认情况下,每个页面请求只会更新&lt;body&gt; 元素,而不是&lt;head&gt;(更多信息可以在此jQuery Mobile demo page 上找到)。

    该页面上引用了一些解决方案:

    构建 jQuery Mobile 网站时最简单的方法是在每个页面的头部引用相同的样式表和脚本集。如果您需要为特定页面加载特定的脚本或样式,我们建议将逻辑绑定到pageinit 事件(详情如下)以在创建特定页面时运行必要的代码(可以通过其id 属性确定,或许多其他方式)。

    另一种页面特定脚本的方法是在未定义 data-role=page 元素时将脚本包含在 body 元素的末尾,或在第一个 data-role=page 元素内。

    最终由您决定哪种方法最适合您的特定用例。

    【讨论】:

    • 感谢萨米尔提供的信息。我在我的 index.html 内部页面“house”中写道,并在rel="external" 中写道,它设法解决了这个问题。但我会在未来考虑您的评论
    猜你喜欢
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多