簡體   English   中英

使用jquery打開后,對話框自動關閉

[英]Dialog closes automatically after opening with jquery

我正在修改有關創建模態的示例

我想使用JavaScript來顯示對話框。 我擁有的代碼打開了對話框,但在1或2秒后關閉。 檢查控制台,我沒有收到任何錯誤。

這是html代碼。 我有一個jsbin,您可以在其中觀察對話框自動關閉。 http://jsbin.com/UDIGeveg/1/edit

<body>
    <a id="openModal" href="">Open Modal</a>
    <div id="openM" class="modalDialog">
        <div>
            <a href="#close" title="Close" class="close">X</a>
            <h2>Modal Box</h2>
            <p>xxxxxxxxxxxxxxxxxxxxxx</p>   
        </div>
    </div>
</body>

這是我的JavaScript

$('#' +"openModal").click(function(){
  document.location.href='#'+"openM";
});

$('#' + "openModal")是錨標記,它的默認操作是將您定向到另一個鏈接。 因此,您需要防止使用e.preventDefault()觸發其默認操作。

$('#' +"openModal").click(function(e){
  e.preventDefault();
  document.location.href='#'+"openM";
});

您可以使用該庫為您檢測網址更改!

<a id="openModal" href="#openM">Open Modal</a>

然后注釋掉您的JavaScript:

/*
$('#' +"openModal").click(function(){
    document.location.href='#'+"openM";
});
*/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM