2

我在同一页面上有多个图像,对于每个图像,单击时,我试图打开一个对话框,我的 HTML 设置中有以下 6 个。当前,当我单击图像时,会弹出 6 个对话框,所有对话框都具有在第一个 div 中找到的相同信息。如何修改我的脚本以使其正常工作: HTML:

<div class="profiles">
    <a class="open" href="#">
        <img src="/../.jpg" / class="img-full"></a>
    <div class="dialog" title="Basic modal dialog">
        <p><strong>Some Text</strong></p>
        <p>
            <strong>Phone</strong>: *********<br />
            <strong>Email</strong>: <a href="mailto:some@email.com">SomeEmail</a>
        </p>
    </div>   
</div>

jQuery:

<script type="text/javascript">
        $(".dialog").dialog({
            autoOpen: false,
            draggable: false,
            position: "center",
            width: "300px",
            modal: true,
            title: "",
            buttons: {
                "Close": function () {
                    $(this).dialog("close");
                }
            }
        });

        $(".open")

          .click(function () {
              $(".dialog").dialog("open");
          });

    </script>
4

1 回答 1

1

因为你有很多.dialogdiv。只保留一个div。

<div class="dialog" title="Basic modal dialog">
    <p><strong>Some Text</strong>

    </p>
    <p> <strong>Phone</strong>: *********
        <br /> <strong>Email</strong>: <a href="mailto:some@email.com">SomeEmail</a>

    </p>
</div>
<div class="profiles"> <a class="open" href="#">
        <img src="/../.jpg" class="img-full"></a>

</div>
<div class="profiles"> <a class="open" href="#">
        <img src="/../.jpg" class="img-full"></a>

</div>

检查这个小提琴。


更新:将您的 js 修改为此。

$(".open").click(function () {
    var div = $(this).next("div.dialog");
    var dia = $(div).dialog({
        draggable: false,
        position: "center",
        width: "300px",
        modal: true,
        title: "",
        buttons: {
            "Close": function () {
                $(this).dialog("close");
                $(this).dialog("destroy"); //need to remove the created html. otherwise the next click will not work.
            }
        }
    });
});

不要忘记添加css

.dialog {
  display:none;
}

小提琴

干杯!!

于 2013-09-27T13:08:05.810 回答