为什么内容被附加到错误的动态生成的选项卡?

我正在尝试动态创建 jQuery 选项卡。它应该可以工作,以便每次我单击“生成”乘法表按钮时,都应创建一个新选项卡,并在此选项卡中显示包含当前数据参数的表。

目前,第一个选项卡存储所有表,后续选项卡不存储任何内容:

https://img3.mukewang.com/64ded4b10001db1c06450265.jpg

如何使第一个选项卡仅存储第一个表,第二个选项卡存储下一个表,依此类推?


$( document ).ready(function() {


  var tabs = [];

  

//Function to generate a multiplication table based on user's range input.

function generateTable(minCol, maxCol, minRow, maxRow) {

  let tabsList = document.getElementById("tabsList");

  let tableTabs = document.getElementById("tableTabs");


  let tabObject = { 

    name: tabs.length, 

    minCol: minCol,

    maxCol: maxCol,

    minRow: minRow,

    maxRow: maxRow

  };

  tabs.push(tabObject);


  let listItem = document.createElement("li");

  let anchor = document.createElement("a");

  anchor.href = `#tab-${tabs.length-1}`;

  anchor.innerText = `tab-${tabs.length-1}`;

  listItem.appendChild(anchor);

  tabsList.appendChild(listItem);

  listItem.classList.add("ui-tabs-tab");


  let tableDiv = document.createElement("div");

  tableDiv.id = `tab-${tabs.length-1}`;

  tableTabs.appendChild(tableDiv);


  var error = document.getElementById("message");


  var table = document.createElement("table");

  var result = "";

  //creating a multTable

  for(var i=minRow; i<=maxRow;i++)

  {

      if(i==minRow)

      {

        result += "<tr>";

        result += "<th>&times;</th>";

      }


      for(var j=minCol; j<=maxCol; j++)

      {

          if(i==minRow || j==minCol)

          {

            if(i==minRow)

              result += "<th>" + j + "</th>";

            else

              result += "<td>"+ (i-1)*j + "</td>";

          }

          else

            result += "<td>"+ (i-1)*j + "</td>";

      }

        result += "</tr>";

        result += "<tr>";

        result += "<th>" + i + "</th>";

        if(i==maxRow)

        {

          for(var j=minCol; j<=maxCol; j++)

          {

            result += "<td>"+ i*j + "</td>";

          }

        }

    }

  //printing the table

  table.innerHTML=result;

  tableDiv.appendChild(table);

  $("#tableTabs").tabs( { "active" : tabs.length-1});

  return false;

}



小怪兽爱吃肉
浏览 79回答 1
1回答

UYOU

选项卡的重新初始化失败。如果它们首先存在,你需要摧毁它们。if ($('#tableTabs').tabs()) {&nbsp; &nbsp; $("#tableTabs").tabs('destroy');}$("#tableTabs").tabs({&nbsp; "active": tabs.length - 1});另请注意,您的表格标记不完整。它缺少结束表行标记:<tr><th>&times;</th><th>-50</th></tr><tr><th>-50</th><td>2500</td>$( document ).ready(function() {&nbsp; var tabs = [];&nbsp;&nbsp;//Function to generate a multiplication table based on user's range input.function generateTable(minCol, maxCol, minRow, maxRow) {&nbsp; let tabsList = document.getElementById("tabsList");&nbsp; let tableTabs = document.getElementById("tableTabs");&nbsp; let tabObject = {&nbsp;&nbsp; &nbsp; name: tabs.length,&nbsp;&nbsp; &nbsp; minCol: minCol,&nbsp; &nbsp; maxCol: maxCol,&nbsp; &nbsp; minRow: minRow,&nbsp; &nbsp; maxRow: maxRow&nbsp; };&nbsp; tabs.push(tabObject);&nbsp; let listItem = document.createElement("li");&nbsp; let anchor = document.createElement("a");&nbsp; anchor.href = `#tab-${tabs.length-1}`;&nbsp; anchor.innerText = `tab-${tabs.length-1}`;&nbsp; listItem.appendChild(anchor);&nbsp; tabsList.appendChild(listItem);&nbsp; listItem.classList.add("ui-tabs-tab");&nbsp; let tableDiv = document.createElement("div");&nbsp; tableDiv.id = `tab-${tabs.length-1}`;&nbsp; tableTabs.appendChild(tableDiv);&nbsp; var error = document.getElementById("message");&nbsp; var table = document.createElement("table");&nbsp; var result = "";&nbsp; //creating a multTable&nbsp; for(var i=minRow; i<=maxRow;i++)&nbsp; {&nbsp; &nbsp; &nbsp; if(i==minRow)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; result += "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; result += "<th>&times;</th>";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; for(var j=minCol; j<=maxCol; j++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==minRow || j==minCol)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i==minRow)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += "<th>" + j + "</th>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += "<td>"+ (i-1)*j + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += "<td>"+ (i-1)*j + "</td>";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; result += "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; result += "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; result += "<th>" + i + "</th>";&nbsp; &nbsp; &nbsp; &nbsp; if(i==maxRow)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var j=minCol; j<=maxCol; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += "<td>"+ i*j + "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; //printing the table&nbsp; table.innerHTML=result;&nbsp; tableDiv.appendChild(table);&nbsp; if ($('#tableTabs').tabs()) {&nbsp; &nbsp; &nbsp; $("#tableTabs").tabs('destroy');&nbsp; }&nbsp; $("#tableTabs").tabs( { "active" : tabs.length-1});&nbsp; return false;}//Function to validate user's input$(function() {&nbsp; $("#inputForm").submit(function(e) {&nbsp; &nbsp; e.preventDefault();&nbsp; }).validate({&nbsp; &nbsp; &nbsp; submitHandler: function(form) {&nbsp; &nbsp; &nbsp; var minCol = parseInt(document.getElementById("minCol").value);&nbsp; &nbsp; &nbsp; var maxCol = parseInt(document.getElementById("maxCol").value);&nbsp; &nbsp; &nbsp; var minRow = parseInt(document.getElementById("minRow").value);&nbsp; &nbsp; &nbsp; var maxRow = parseInt(document.getElementById("maxRow").value);&nbsp; &nbsp; &nbsp; generateTable(minCol, maxCol, minRow, maxRow);&nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; });//end validate});//end function});html {&nbsp; background-color: #9FA5FF;&nbsp; height: 100%;}body {&nbsp; background-color: #E2E3FF;&nbsp; margin: 0 auto;&nbsp; width: 70%;}h2 {&nbsp; padding-top: 20px;&nbsp; padding-bottom: 20px;&nbsp; text-align: center;&nbsp; background-color: #c68de2;}h6 {&nbsp; text-align: center;&nbsp; margin-bottom: 0px;}.purple-background {&nbsp; background-color:#c68de2;}.black {&nbsp; color:#000000;}.inputfield{&nbsp; height:30px;}.containerInput {&nbsp; padding-left: 0px;&nbsp; padding-right: 0px;}.container {&nbsp; padding: 20px 0;&nbsp; overflow: scroll;&nbsp; height: 400px;}#multTable {&nbsp; margin: auto;}#multTable td, th {&nbsp; width: 50px;&nbsp; font-size: 20px;&nbsp; border: 1px solid blue;}#multTable td:nth-child(even) {&nbsp; background-color: #ffffff;}#multTable th {&nbsp; background-color: #9FA5FF;}#message p{&nbsp; color: red;&nbsp; margin-bottom: 0px;&nbsp; padding-top: 15px;&nbsp; padding-left: 15px;&nbsp; text-align: center;}.error {&nbsp; color: red;}<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>Multiplication table</title>&nbsp; &nbsp; <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">&nbsp; &nbsp; <link rel="stylesheet" href="css/style.css">&nbsp; &nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.min.css" integrity="sha512-9h7XRlUeUwcHUf9bNiWSTO9ovOWFELxTlViP801e5BbwNJ5ir9ua6L20tEroWZdm+HFBAWBLx2qH4l4QHHlRyg==" crossorigin="anonymous" />&nbsp; &nbsp; <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" integrity="sha512-aOG0c6nPNzGk+5zjwyJaoRUgCdOrfSDhmMID2u4+OIslr0GjpLKo7Xm0Ao3xmpM4T8AmIouRkqwj1nrdVsLKEQ==" crossorigin="anonymous" />&nbsp; &nbsp; <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; &nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>&nbsp; &nbsp; <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script src="js/validate.js"></script>&nbsp; </head>&nbsp; <body><div class="containerInput">&nbsp; <h2>Multiplication Table</h2>&nbsp; <div class="row justify-content-center">&nbsp; &nbsp; <div class="col-lg-6 col-md-6-offset-3 col-sm-12 well" >&nbsp; &nbsp; <form id="inputForm" class = 'card p-3 bg-light' class="form-horizontal">&nbsp; &nbsp; &nbsp; <h6>Please enter range values for your Multiplication Table.</h6>&nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label col-sm-10" for="minCol">Minimum Column Value:</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control inputfield" id="minCol" name="minCol" value="-50">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="minColSlider" class="sliderMC"></div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label col-sm-10" for="maxCol">Maximum Column Value:</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control inputfield" id="maxCol" name="maxCol" value="-50">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="maxColSlider" class="sliderMC2"></div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label col-sm-10" for="minRow">Minimum Row Value:</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control inputfield" id="minRow" name="minRow" value="-50">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="minRowSlider" class="sliderMR"></div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <label class="control-label col-sm-10" for="maxRow">Maximum Row Value:</label>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="form-control inputfield" id="maxRow" name="maxRow" value="-50">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="maxRowSlider" class="sliderMR2"></div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-offset-2 col-sm-10">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="submit" class="btn btn-secondary purple-background black">Generate</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form>&nbsp; </div>&nbsp;</div></div><div id="message"></div>&nbsp;<div id="tableTabs">&nbsp; <ul id="tabsList">&nbsp; </ul></div>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript