javascript - 如何为动态表创建弹出窗口

标签 javascript jquery html css dynamic

HTML

    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>


<!--To display JSON data in the tables-->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script> $(function() {
    var dmJSON = "data.json";
    $.getJSON( dmJSON, function(data) {
        $.each(data.records, function(i, f) {
            var $table="<table  class='mystyles' table border=5><tbody><tr>" + "<td>" + f.Clue + "</td></tr>" + "<tr><td>" + f.Answer + "</td></tr>" + "<tr><td>" + f.Status + "</td></tr>" + "<tr><td> " + f.Views + "</td></tr>" + "</tbody>&nbsp;&nbsp;&nbsp;</table>"
            $("#entrydata").append($table)
        });
    });
});

<!--Popup function-->
             $(function() {
    $( "#dialog" ).dialog();
  });

        </script>

    </head>
    <body>
               <div id="popup-box1" class="popup-position">
        <div id="popup-wrapper">
            <p style="text-align: right;"><a href="javascript:void(0)" onclick="toggle_visibility('popup-box1');">X</a></p>
            <div id="popup-container">
                <h3>Popup box 1</h3>
                <p>A popup box</p>
            </div><!-- Popup container end -->
            <p style="text-align: right;"><a href="javascript:void(0)" >Close popup box</a></p>
        </div><!-- Popup wrapper popup end -->
    </div><!-- Popup box 1 end -->


        <!--Creating a dynamic table-->
          <a href="javascript:void(0)" onclick="toggle_visibility('popup-box1');">
              <div class="wrapper" >
                    <div class="profile">
                      <div id='entrydata' >

                        </div>
                  </div>
                </a>
          </div>

代码动态创建表并显示 JSON 数据。我已经添加了一个弹出窗口,但问题是我需要为每个表使用不同的弹出窗口。例如:对于第一个表,我想要弹出窗口 1,第二个表应该显示弹出窗口 2。由于这些表是动态创建的,所以我不知道如何向其中添加不同的弹出窗口。对此的任何解决方案将不胜感激。提前致谢

最佳答案

您可以像这样为表分配唯一 ID。

<!--To display JSON data in the tables-->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script> $(function() {
                     var dmJSON = "data.json";
                     $.getJSON( dmJSON, function(data) {
                         var idx=1;
                         $.each(data.records, function(i, f) {
                             var myid = 'mytable'+String(idx);
                             idx++;
                             var $table="<table  id='"+myid+"' class='mystyles' table border=5><tbody><tr>" + "<td>" + f.Clue + "</td></tr>" + "<tr><td>" + f.Answer + "</td></tr>" + "<tr><td>" + f.Status + "</td></tr>" + "<tr><td> " + f.Views + "</td></tr>" + "</tbody>&nbsp;&nbsp;&nbsp;</table>"
                             $("#entrydata").append($table)
                         });

                     });
                 });

关于javascript - 如何为动态表创建弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30259066/

相关文章:

javascript - 如何为每个具有附加数字的 id 的元素使用 Javascript 函数

javascript - 如何在javascript中动态创建的行中获取两列的差异

jquery - 同位素 : positioning of elements that have different heights

JavaScript 检测复选框并选中并执行操作

javascript - 使用带有自定义 HTML 元素的图像 usemap?

php - 如何向多个渲染的 URL 添加参数?

javascript - 如何在 Uppy XHR 请求中添加自定义帖子字段?

javascript - Foreach 与 'afterAdd' 的绑定(bind)会导致整个模板在插入时再次绘制

javascript - 使用重新图表 React hooks AreaChart 控制台警告

javascript - js中的全局变量,如何避免