【问题标题】:How to make multiple JQuery pop up , on hyperlinks click?如何让多个 JQuery 弹出,点击超链接?
【发布时间】:2013-10-14 11:12:58
【问题描述】:

我想在我点击超链接时弹出一个弹出框(其中包含文本)。我的 html 中有 5 个超链接。这是代码:

<div class="four columns">
  <h4>
    <a id="OpenDialog" href="#" >Open dialog 1</a>
  </h4>
  <img src="one.jpg" />
  <div id="dialog" title="Dialog Title 1">dialog text 1</div>
</div>
<div class="four columns">
  <h4>
    <a id="OpenDialog" href="#" >Open dialog 2</a>
  </h4>
  <img src="two.jpg" />
  <div id="dialog" title="Dialog Title 2">dialog text 2</div>
</div>

我也把它放在我的 html 中:

   <script type="text/javascript">
    $(document).ready(function () {
        $("#OpenDialog").click(function () {
            $("#dialog").dialog({modal: true, height: 590, width: 1005 });
        });
    });
  </script>

我还包括了这个现成的脚本:

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

但问题是弹出功能仅在第一个超链接中起作用。

【问题讨论】:

  • jquery ui 库添加了吗?浏览器控制台中的任何错误
  • 使用“console.log”调试你的代码,你可以看到代码在哪里停止。

标签: jquery hyperlink dialog popup jquery-ui-dialog


【解决方案1】:

我会使用 jquery parent 和 children 来获得你想要的。 (jsfiddle:http://jsfiddle.net/pjVcR/2/

<script>
$("a").click(function(event) {
  $(this).parent().parent().children(".dialog").dialog({
    close: function( e, ui ) {
      $(this).dialog('destroy');
    }
  });
});
</script>

在这种情况下,您必须在开头隐藏 .dialog div。 此外,将对话框容器更改为具有名为“dialog”的类(而不是 id)。这样,您将不会有许多具有相同 id 的 div,并且您的功能将在那里。

这里有一些参考:

  1. http://api.jquery.com/parent/
  2. http://api.jquery.com/children/
  3. Getting the ID of the element that fired an event

【讨论】:

    【解决方案2】:

    首先添加这些文件

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
      <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    

    然后

     <script type="text/javascript">
        $(document).ready(function () {
            $("#OpenDialog").click(function (e) {
            e.preventDefault();
                $("#dialog").dialog({modal: true, height: 590, width: 1005 });
            });
        });
      </script>
    

    参考jQuery dialog box

    【讨论】:

    • 您正在使用具有相同 ID 的多个对话框,因此它会创建问题然后使用类
    • 我还有一些其他的 js 文件我没有使用。我删除了它们。它现在仅针对 Αποστόλης Παππάς 弹出。我应该怎么做才能让它为其他人弹出?不同的 Opendialog 可能吗?例如 Opendialog1 、 Opendialog2 等?
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签