【问题标题】:JQuery Mobile Duplicate Js / Js not work in other pageJQuery Mobile Duplicate Js / Js 在其他页面中不起作用
【发布时间】:2014-07-08 08:23:13
【问题描述】:

我在 Jquery mobile 上实现 Javascript 时遇到问题,每次我从页面返回时,javascript 都会被复制。

示例代码如下:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS duplicated page1</title> 

<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<link rel="stylesheet" href="css/style-mobile.css">

<script src="js/tes.js"></script>
</head>
<body>
    <div class="demo-wrapper" data-role="page" data-theme="a" id="page1"> 
        <div class="header" data-role="header" style="position:fixed; background-color:#004165;">
    <div id="div1">
        <span class="right">Click me</span>
        <div id="option"><span style="color:white;">A B C</span></div>
    </div>
</div>
<div id="separator" style="height:100px;"></div>
    <a href="jsduplicate2.html" data-transition="slide">Go to page2</a>
    </div>
</body>

这是另一个页面:

<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JS duplicated page2</title> 

<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
<link rel="stylesheet" href="css/style-mobile.css">
</head>
<body>
    <div class="demo-wrapper" data-role="page" data-theme="a" id="page2"> 

    <div class="header" data-role="header" style="position:fixed; background-color:#004165;">
    <div id="div1">
        <span class="right">Click me</span>
        <div id="option"><span style="color:white;">A B C</span></div>
    </div>
</div>
<div id="separator" style="height:100px;"></div>
    <a href="jsduplicate1.html" data-transition="slide">Go to page1</a>

</div>
</body>
</html>

这里是自定义的 JS:

$( document ).on( "pageshow", function() {
$(".header #option").toggle('inactive');
$(".header #option").hide();
$(".right").click(function(){   
    $(".header #option").toggle('inactive'); 
    $(".header #option").show();
});
    });

每次我从第 2 页返回时,当我点击它时,切换会运行 2 次。

如果我只在page1中初始化js,当导航到page2时,js不起作用。 请帮忙..

【问题讨论】:

    标签: javascript jquery ajax jquery-mobile


    【解决方案1】:

    第 2 页没有脚本标签,所以它没有被加载。加进去就好了。

    <head>
     <script src="js/tes.js"></script>
    </head>
    

    Demo 为我工作。

    【讨论】:

    • 如果你给脚本标签,当回到第1页时,切换运行2次(2个动画)。
    猜你喜欢
    • 2012-08-29
    • 2016-01-03
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2017-08-28
    相关资源
    最近更新 更多