Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么document.getElementById不工作? document.getElementById('r'+a.toString()).innerHTML+=“a+”X“+n+”=“a*n+”;_Javascript_Html Table - Fatal编程技术网

Javascript 为什么document.getElementById不工作? document.getElementById('r'+a.toString()).innerHTML+=“a+”X“+n+”=“a*n+”;

Javascript 为什么document.getElementById不工作? document.getElementById('r'+a.toString()).innerHTML+=“a+”X“+n+”=“a*n+”;,javascript,html-table,Javascript,Html Table,我无法使用getElementById().innerHTML和创建表。上面的代码不起作用。使用控制台日志查看您的元素是否存在并被找到。 如果代码不起作用,那么很可能 1) 找不到该元素 2) 元素id字符串不是您认为的那样 document.getElementById('r'+a.toString()).innerHTML+="<td><tr>"+a+"</tr>X<tr>"+n+"</tr>=<tr>"+a*n+"&l

我无法使用
getElementById().innerHTML
创建表。上面的代码不起作用。

使用控制台日志查看您的元素是否存在并被找到。 如果代码不起作用,那么很可能 1) 找不到该元素 2) 元素id字符串不是您认为的那样

document.getElementById('r'+a.toString()).innerHTML+="<td><tr>"+a+"</tr>X<tr>"+n+"</tr>=<tr>"+a*n+"</tr></td>";
let elementId='r'+a.toString();

log('element id为:',elementId)//欢迎来到Stackoverflow。请提供您的HTML,以便我们能够帮助您。您在
标记中有
标记。这是无效的HTML。你能帮我用getelementbyid.innerELEMENT创建一个3的多应用程序表并在innerHTML中创建一个标记吗。。。先生,我正在学习。
let elementId = 'r'+a.toString();
console.log('element id is:', elementId);//<- what is the elementId
let element = document.getElementById(elementId);
console.log('element is:', element);// <- did you find the element
element.innerHTML+="your html here";
//was there an error in console.
//  HTML
<section>
  <p>Multiplication table</p>
  <button onclick="createTable()">Create Table</button>
  <input type="number" value="3" id="size" placeholder="number of rows">
  <table id="multiplication-table"></table>
</section>
// CSS
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
  color: white;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}
table td {
  border: 1px solid white;
  padding: 0.2em;
  margin: 1em;
}
// JavaScript
window.createTableRow = function(rowNr, size) {
  let row = '<tr>';
  for (i = 1; i <= size; i++) {
    row += '<td>' + (rowNr * i) + '</td>';
  }
  return row + '</tr>';
}
window.createTable = function() {
  let size = parseInt(document.getElementById("size").value);
    let table = document.getElementById("multiplication-table");
  table.innerHTML = '<tbody></tbody>';
  let i;
  for (i = 1; i <= size; i++) {
    let row = createTableRow(i, size);
    table.innerHTML += row;
  }
}