当前位置: 代码迷 >> JavaScript >> 为什么默认情况下在移动设备上会显示模式弹出窗口? 为什么它不能按预期工作?
  详细解决方案

为什么默认情况下在移动设备上会显示模式弹出窗口? 为什么它不能按预期工作?

热度:53   发布时间:2023-06-05 09:37:45.0

我的模式弹出窗口在移动设备上加载时,默认情况下是可见的,因此,当页面加载时,即使我没有单击应该激活它的按钮,在页面顶部也存在模式弹出窗口。 另外,按钮不起作用。 我以为Dialog Tag和JavaScript无法在移动设备上运行一定是有问题的,但是我该如何解决? 这是我的代码:

<dialog id="window" style="height:520px;">
    <div id="warning-box">
        <h3>Confirm</h3>  
        <p>Are you sure you want to return to Home? You will be logged out of your account.</p>
        <div id="button-container2">
            <button id="exit" class="popupbutton1">Cancel</button>
            <button id="confirm1" class="popupbutton1">Yes</button>
        </div>
    </div>
</dialog>
<script>
        (function () {
            var dialog = document.getElementById('window');
            document.getElementById('show').onclick = function () {
                dialog.show();
            };
            document.getElementById('exit').onclick = function () {
                dialog.close();
            };
            document.getElementById('confirm1').onclick = function () {
                dialog.close();
                location.href = "../Account/Login/logout.php";
            }
        })();
</script>

任何帮助表示赞赏。 谢谢

如果您使用的是jQuery Mobile,则不建议使用该对话框小部件( ),并且有一种新方法。 下面的示例代码,请在此尝试。 还要注意,使用jQuery可以将所有的“ document.getElementById”调用替换为“ $”。 以便于阅读!

<div data-role="page" id="mainPage">
  <button id="showDialog" class="popupbutton1">Show Dialog</button>
</div>
<div data-role="page" id="dialogPage">
<div id="warning-box">
     <h3>Confirm</h3> 
    <p>Are you sure you want to return to Home? You will be logged out of your account.</p>
    <div id="button-container2">
        <button id="exit" class="popupbutton1">Cancel</button>
        <button id="confirm1" class="popupbutton1">Yes</button>
    </div>
</div>

$('#showDialog').click(function () {
  $("body").pagecontainer("change", "#dialogPage", {
    role: "dialog"
  });
});

$('#exit').click(function () {
  $("#dialogPage").dialog("close");
  alert('cancelled');
});
$('#confirm1').click(function () {
  $("#dialogPage").dialog("close");
  alert('confirmed!');
});

(编辑:)您仍然可以使用对话框小部件来执行此操作,请参见此

  相关解决方案