0

ページが読み込まれるたびに jQuery モバイル ダイアログが開きます。Cookie が一度だけ開かれ、二度と開かないように設定されていても、更新のたびに開き続けます。トリガーなしでロードする理由がわかりませんか?どんな助けでも大歓迎です。

ジャバスクリプト

function openDialog() {
var interval = setInterval(function(){
       $.mobile.changePage('#dialog');
       clearInterval(interval);
},1);  
}

$(function() {
if ($.cookie('dialog_shown') == null) {
   $.cookie('dialog_shown', 'yes', { expires: 7, path: '/' });
        $(document).on('pageshow', '#index', function(){   
            openDialog();   
        }); 
    } 
});

HTML

<body>
<div data-role="dialog">

    <div data-role="header" data-theme="d">
        <h1>Custom Dialog</h1>
    </div>

    <div data-role="content">
        <h1>Customize the HTML. Have any content you want in here.</h1>
        <p>This is a regular page, styled as a dialog. To create a dialog, just link to a normal page and include a transition and <code>data-rel="dialog"</code> attribute.</p>
        <a href="#index" data-role="button" data-theme="b">Button Style</a>       
        <a href="#index" data-role="button" data-theme="c">Cancel</a>    
    </div>
</div>

<div data-role="page" id="index">
</div>
</body>
4

1 に答える 1

0

jquery mobile は、ページが選択されていない場合、本文の最初のページまたはダイアログを読み込みます。したがって、HTML は次のようになります。

<body>

   <div data-role="page" id="index">
   </div>

   <div data-role="dialog">
    .......
   </div>

</body>

2 つ目は、Cookie が存在しない場合、undefinednotを返すことnullです。それで:

if ($.cookie('dialog_shown') === undefined) { ..}

3 つ目は、ダイアログの ID を設定していないことです。

<div data-role="dialog" id="dialog">

コードは次のようになります。

   $(document).delegate('#index','pageshow',  function(){   
      if ($.cookie('dialog_shown') === undefined) {
           $.cookie('dialog_shown', 'yes', { expires: 7, path: '/' });
           openDialog();
      }      
   }); 
于 2013-05-09T23:29:10.827 に答える