简体   繁体   English

jQuery ajax调用后javascript无法正常工作

[英]javascript not working after jquery ajax call

i am developing my first mobile web app using jquery, 我正在使用jquery开发我的第一个移动网络应用程序,

When i press a button to go to the next page it does but the javascript on the page doesnt work. 当我按一个按钮转到下一页时,它可以,但是页面上的javascript不起作用。

Here is my code , As before my header is a php doc so i dont have to recall all the time header.php 这是我的代码,就像我的标头是php doc一样,所以我不必一直都记得header.php

<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">-->
<!DOCTYPE html>
<html manifest="cache.manifest">
<!--<html>-->
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>Balti Tandoori</title>

    <meta name="apple-mobile-web-app-capable" content="yes" />

    <link rel="apple-touch-icon" href="iphone.png" />
    <link rel="apple-touch-startup-image" href="startup.png" />
<!--<meta name="apple-mobile-web-app-status-bar-style" content="black" />-->



    <link rel="stylesheet" href="js/jquery.mobile-1.1.0.min.css" />
            <!-- App custom styles -->



     <link rel="stylesheet" href="js/balti.min.css" />
     <link href="css/mobile.css" rel="stylesheet" type="text/css" />

    <script src="js/jquery-1.7.1.min.js"></script>
    <script src="js/jquery.mobile-1.1.0.min.js"></script> 

            <style type="text/css" media="screen">



        h2 {
            margin: 0;
        }

        .drawers-wrapper {
            position: relative;
            width: 100%;

        }

        .drawer {
        background-color:#ffffff;

            color:#ffffff;
            font-size:20px;
            line-height:1.3em;

        }
<!-- NO REQUIRED -->
        .boxcap {
            height:5px;
            left:0pt;
            position:absolute;
            width:100%;
            z-index:100;
            background:transparent url(http://images.apple.com/downloads/images/sidenav_capbottom.png) no-repeat scroll 0%;
            margin-top:-5px;
        }

        .captop {
            background-image:url(http://images.apple.com/downloads/images/box_188captop.png );
            bottom:auto;
            top:0pt;
            margin-top:0;
        }
<!-- END JUST DOES CORNERS -->
        .drawers {
            margin-bottom:15px;
            color:#76797C;
            font-size:11px;
            line-height: 18px;

        }

        .drawers A {
            color:#666666;
            text-decoration:none;
            font-family:"Lucida Grande",Geneva,Arial,Verdana,sans-serif;
            font-size-adjust:none;
            font-style:normal;
            font-variant:normal;
            font-weight:normal;
        }

        .drawer li {
            border-bottom:1px solid #E5E5E5;
            line-height:16px;
            padding:6px 0pt;
        }

        UL {
            list-style: none;
            padding: 0;
        }

        UL.drawers {
            margin: 0;
        }

        .drawer-handle {
            background-color:#000000;

            color:#ffffff;
            cursor:default;
            font-size:20px;
            font-weight:normal;
            height:50px;
            line-height:25px;
            margin-bottom:0pt;
            text-indent:15px;
            width:100%;
            border:1px solid #ffffff
        }

        .drawer-handle.open {
        background:transparent url(css/body.gif) no-repeat;


        }

        .drawer UL {
            padding: 0 12px;
            padding-bottom:0pt;
        }

        .drawer-content UL {
            padding-top: 7px;
        }

        .drawer-content LI A {
            display:block;
            overflow:hidden;

        }

        .alldownloads li {
            border:0pt none;
            line-height:18px;
            padding:0pt;

        }

        </style>




        <script src="jquery.dimensions.js" type="text/javascript"></script>
    <script src="jquery.accordion.js" type="text/javascript"></script>  






    <!-- link to allow bookmark website -->
    <link rel="stylesheet" href="bookmark/add2home.css">
    <script type="text/javascript">
        var addToHomeConfig = {
        animationIn: 'bubble',
        animationOut: 'drop',
        lifespan:10000,
        expire:2,
        touchIcon:true,
        message:'Add Our Mobile App To Your <strong>%device</strong> Home Page. Available Anytime `%icon`.'
        };
    </script>
    <script type="application/javascript" src="bookmark/add2home.js"></script>
    <!-- bookmark end -->

   <script type="text/javascript">
        $(document).bind("mobileinit", function() {
        $.mobile.page.prototype.options.addBackBtn = true;
        });    
    </script>  
<script type="text/javascript">

$(function () {
  $('UL.drawers').accordion({
    // the drawer handle
    header: 'H2.drawer-handle',

    // our selected class
    selectedClass: 'open',

    // match the Apple slide out effect
    event: 'mouseover'
  });
});

</script>


    </head>

this is my index.php 这是我的index.php

<?
require_once("header.php");
?>
<!-- do not touch above -->
    <body>
        <div data-role="page" id="page1" data-theme="f">
            <div data-theme="f" data-role="header">
                <img src="css/logo.gif"width="100%" style="border-bottom :1px solid white" alt="" />
            </div>
         <div data-role="content">
                <ul data-role="listview" data-divider-theme="b" data-inset="true">
                    <li data-theme="b">
                        <a href="menu.php" data-transition="flip">
                            Menu
                        </a>
                    </li>
                    <li data-theme="b">
                        <a href="#page1" data-transition="flip">
                            Opening Times
                        </a>
                    </li>
                    <li data-theme="b">
                        <a href="#page1" data-transition="flip">
                            How To Find Us
                        </a>
                    </li>
                </ul>
                <a data-role="button" data-transition="pop" href="page1" data-theme="b" data-icon="tel" data-iconpos="left">
                    Call Us
                </a>
         <!--   </div>-->


    </div>

            <!-- do not touch below -->

            <div data-theme="b" data-role="footer">
                <h3>
                    &copy; Balti Tandoori
                </h3>
            </div>
        </div>
        <script>
            //App custom javascript
        </script>
    </body>
</html>

this is the page the index calls where the javascript does not run, It will run if i disable the ajax call from the button like this But i want the ajax to work as well, How do i recall the javascript on load etc 这是索引无法在其中运行javascript的页面,如果我从这样的按钮禁用ajax调用,它将运行,但是我希望ajax也能正常运行,如何在加载时调用javascript等

<a href="javascript:window.location.href='menu.php'"  data-ajax='false'

menu.php menu.php

<?
require_once("header.php");
?>
<!-- do not touch above -->
    <body>



        <div data-role="page" id="page1" data-theme="f">
            <div data-theme="f" data-role="header">
                <img src="css/logo.gif"width="100%" style="border-bottom :1px solid white" alt="" />
            </div>
         <div data-role="content">
         <a href="index.php"  data-role="button" data-transition="flip" data-icon="back" data-theme="b">Back</a>
                              <a data-role="button" data-transition="pop" href="page1" data-theme="b" data-icon="tel" data-iconpos="left">
                    Call Us
                </a>
         <!--   </div>-->


                          <div class="drawers-wrapper">
    <!--<div class="boxcap captop"></div>-->
    <ul class="drawers">

 <ul class="drawers">
        <li class="drawer">
            <h2 class="drawer-handle open">Starters</h2>
     <!-- removes under border-->
           <!-- <ul class="alldownloads">-->
            <!--removed above -->
            <ul>
                <li id="sn-downloadsmacosx">All Categories <div style="text-align:right;">£2.50</div></li>
                <li id="sn-aperture">Big dish<div style="text-align:right;">£2.50</div></li>
                <li id="sn-apple"><a href="/downloads/macosx/apple/">Apple</a></li>

                <li id="sn-video"><a href="/downloads/macosx/video/">Video</a></li>
                <li id="sn-dashboard"><a href="/downloads/dashboard/" class="bottom">Widgets</a></li>
            </ul>
        </li>
        <li class="drawer">
            <h2 class="drawer-handle">Tandoori</h2>
            <ul>
                <li><a title="iTunes 7.5" href="http://www.apple.com/itunes/download/">1. iTunes 7.5</a></li>
                <li><a title="QuickTime 7.3.1" href="http://www.apple.com/quicktime/download/">2. QuickTime 7.3.1</a></li>

                <li><a title="iSquint" href="/downloads/macosx/ipod_itunes/isquint.html">13. iSquint</a></li>
                <li class="last"><a title="US Holiday Calendar" href="/downloads/macosx/calendars/usholidaycalendar.html">14. US Holiday Calendar</a></li>
            </ul>
        </li>
          <li class="drawer">
            <h2 class="drawer-handle">Traditional</h2>
            <ul>
                <li><a title="iTunes 7.5" href="http://www.apple.com/itunes/download/">1. iTunes 7.5</a></li>
                <li><a title="QuickTime 7.3.1" href="http://www.apple.com/quicktime/download/">2. QuickTime 7.3.1</a></li>

                <li><a title="iSquint" href="/downloads/macosx/ipod_itunes/isquint.html">13. iSquint</a></li>
                <li class="last"><a title="US Holiday Calendar" href="/downloads/macosx/calendars/usholidaycalendar.html">14. US Holiday Calendar</a></li>
            </ul>
        </li>
          <li class="drawer">
            <h2 class="drawer-handle">Originals</h2>
            <ul>
                <li><a title="iTunes 7.5" href="http://www.apple.com/itunes/download/">1. iTunes 7.5</a></li>
                <li><a title="QuickTime 7.3.1" href="http://www.apple.com/quicktime/download/">2. QuickTime 7.3.1</a></li>

                <li><a title="iSquint" href="/downloads/macosx/ipod_itunes/isquint.html">13. iSquint</a></li>
                <li class="last"><a title="US Holiday Calendar" href="/downloads/macosx/calendars/usholidaycalendar.html">14. US Holiday Calendar</a></li>
            </ul>
        </li>
          <li class="drawer">
            <h2 class="drawer-handle">Specials</h2>
            <ul>
                <li><a title="iTunes 7.5" href="http://www.apple.com/itunes/download/">1. iTunes 7.5</a></li>
                <li><a title="QuickTime 7.3.1" href="http://www.apple.com/quicktime/download/">2. QuickTime 7.3.1</a></li>

                <li><a title="iSquint" href="/downloads/macosx/ipod_itunes/isquint.html">13. iSquint</a></li>
                <li class="last"><a title="US Holiday Calendar" href="/downloads/macosx/calendars/usholidaycalendar.html">14. US Holiday Calendar</a></li>
            </ul>
        </li>
        <li class="drawer last">
            <h2 class="drawer-handle">Fish</h2>
            <ul>
                <li><a title="iTunes 7.5" href="http://www.apple.com/itunes/download/">1. iTunes 7.5</a></li>
                <li><a title="QuickTime 7.3.1" href="http://www.apple.com/quicktime/download/">2. QuickTime 7.3.1</a></li>

                <li><a title="Security Update 2007-009 1.1 (10.4.11 Universal)" href="/downloads/macosx/apple/security_updates/securityupdate20070091110411universal.html">13. Security Update 20</a></li>
                <li class="last"><a title="Security Update 2007-009 1.1 (10.5.1)" href="/downloads/macosx/apple/security_updates/securityupdate2007009111051.html">14. Security Update 20</a></li>
            </ul>
        </li>
    </ul>   
<!--<div class="boxcap"></div>-->
    </div>  



            </div>      



            <!-- do not touch below -->
            <div data-theme="b" data-role="footer">
                <h3>
                    &copy; Balti Tandoori
                </h3>
            </div>

    </body>
</html>

Many thanks for your help 非常感谢您的帮助

added for clarity to header.php 为清楚起见添加到header.php

       <script type="text/javascript">

  $(document).bind('pageinit', function(){  //new bit added in script
$(function () {
  $('UL.drawers').accordion({
    // the drawer handle
    header: 'H2.drawer-handle',

    // our selected class
    selectedClass: 'open',

    // match the Apple slide out effect
    event: 'mouseover'
  });
});
});          //new bit added in script
</script>

Which JavaScript isn't working? 哪个 JavaScript不起作用? I'm having a hard time getting exactly what it is that you mean, but I'm willing to take a hunch that none of your event handlers are getting bound. 我很难理解您的意思,但是我很想知道您的事件处理程序没有任何约束。 You don't show what the contents of all those external script files contain, so I can't say that for sure, but just a quick breakdown: 您没有显示所有这些外部脚本文件的内容,所以我不能肯定地说,只是快速分解:

All of your JavaScript tags are in the head of your document. 您所有的JavaScript标记都位于文档的开头。 That means that they get loaded up before the body tag has been processed. 这意味着它们在处理body标签之前就已加载。 Additionally, jQuery Mobile creates a great deal of the DOM dynamically. 另外,jQuery Mobile动态创建了大量的DOM。 Nowhere do I see a call to $(document).ready() , which is fine because this has been sort of "phased out" in jQuery Mobile, nor do I see a call to $(document).bind("pageinit", function()) , which is the preferred way to bind event handlers to DOM elements after the page has loaded in jQuery Mobile. 我在哪里都看不到对$(document).ready()的调用,这很好,因为在jQuery Mobile中已经$(document).bind("pageinit", function())进行了“逐步淘汰”, 看不到对$(document).bind("pageinit", function())的调用。 $(document).bind("pageinit", function()) ,这是将页面加载到jQuery Mobile之后将事件处理程序绑定到DOM元素的首选方法。

I see you bind a callback to the document's mobileinit event, but per the jQuery Mobile documentation, this is called long before the DOM is built up. 我看到您将回调绑定到文档的mobileinit事件,但是根据jQuery Mobile文档,在建立DOM之前很久就调用了此方法。

Let's say that your accordion function is what isn't working. 假设您的手风琴功能不起作用。 The pattern you're looking for is something like: 您正在寻找的模式是这样的:

$(document).bind('pageinit', function(){
    $('UL.drawers').accordion({
    // the drawer handle
    header: 'H2.drawer-handle',

    // our selected class
    selectedClass: 'open',

    // match the Apple slide out effect
    event: 'mouseover'
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM