简体   繁体   English

我的 Javascript 代码不起作用,这是怎么回事?

[英]My Javascript code won't work, what's wrong with it?

My javascript code won't work one bit and I honestly have no idea why.我的 javascript 代码一点也不起作用,老实说我不知道为什么。 I'm still very new to javascript so if someone can point out where I'm going wrong, I'll very much appreciate it.我对 javascript 还是很陌生,所以如果有人能指出我哪里出错了,我将不胜感激。 It's supposed to be a pixel art maker where you can make a table based on user input and color each separate cell.它应该是一个像素艺术创作者,您可以在其中根据用户输入制作表格并为每个单独的单元格着色。

Edit: Sorry, I should have made myself clearer.编辑:对不起,我应该让自己更清楚。 For example: entering 5 in 'Grid Height' and 5 in 'Grid Width' then clicking the "Submit" button should produce a 5 x 5 table/grid below 'Design Canvas'.例如:在“网格高度”中输入 5,在“网格宽度”中输入 5,然后单击“提交”按钮应该会在“设计画布”下方生成一个 5 x 5 的表格/网格。 But when I do that, the numbers just resets to 1 and no grid is displayed.但是当我这样做时,数字只会重置为 1,并且不会显示任何网格。 Basically, nothing happens.基本上,什么也不会发生。 I know I might be missing a lot of code, but no errors are displayed in DevTools.我知道我可能遗漏了很多代码,但 DevTools 中没有显示任何错误。 For the color, choosing a color from 'Pick A Color' and then clicking on a cell should only fill that cell.对于颜色,从“选择颜色”中选择一种颜色,然后单击一个单元格应该只填充该单元格。 Clicking 'Submit' again should reset the table.再次单击“提交”应该会重置表格。

 var table = document.getElementById("pixelCanvas"); var height = document.getElementById("inputHeight").value; var width = document.getElementById("inputWidth").value; var color = document.getElementById("colorPicker"); var submit = document.getElementById("submit"); var size = document.getElementById("sizePicker"); submit.addEventListener("click", makeGrid); function makeGrid(height, width) { height, width.preventDefault(); table.innerHTML = ""; for (let x = 0; x < height; x++) { var tr = table.insertRow(x); for (let y = 0; y < width; y++) { var td = tr.insertCell(y); table.appendChild(tr); td.addEventListener("click", fillCell); tr.appendChild(td); } table.appendChild(tr); } } function fillCell(event) { event.preventDefault(); event.style.backgroundColor = color.value; }
 body { text-align: center; background-color: white; color: plum; font-family: 'Roboto', sans-serif; } h1 { font-family: 'Roboto', sans-serif; font-size: 70px; margin: 0.2em; } h2 { margin: 1em 0 0.25em; } h2:first-of-type { margin-top: 0.5em; } table, tr, td { border: 2px solid black; } table { border-collapse: collapse; margin: 0 auto; color: black; } tr { height: 20px; } td { width: 20px; } input[type=number] { width: 6em; }
 <h1>Pixel Art Maker</h1> <h2>Choose Grid Size</h2> <form id="sizePicker"> Grid Height: <input type="number" id="inputHeight" name="height" min="1" value="1"> Grid Width: <input type="number" id="inputWidth" name="width" min="1" value="1"> <input type="submit" id="submit"> </form> <h2>Pick A Color</h2> <input type="color" id="colorPicker"> <h2>Design Canvas</h2> <table id="pixelCanvas"></table>

When you click on a cell, there is no need to prevent default.单击单元格时,无需防止默认。 You will need to access the event target first, before accessing the style property.在访问样式属性之前,您需要先访问事件目标。

Also, add the submit event to the form, rather than to the button.此外,将提交事件添加到表单,而不是按钮。 You can access form elements off of the form, so that you do not need to store so many globals.您可以访问表单外的表单元素,这样您就不需要存储那么多全局变量。

 const form = document.getElementById("sizePicker"), table = document.getElementById("pixelCanvas"), color = document.getElementById("colorPicker"); form.addEventListener("submit", makeGrid); function makeGrid(e) { const form = e.target, width = form.elements.inputWidth.value, height = form.elements.inputHeight.value; table.innerHTML = ""; for (let x = 0; x < height; x++) { var tr = table.insertRow(x); for (let y = 0; y < width; y++) { var td = tr.insertCell(y); table.appendChild(tr); td.addEventListener("click", fillCell); tr.appendChild(td); } table.appendChild(tr); } } function fillCell(e) { e.target.style.backgroundColor = color.value; }
 body { text-align: center; background-color: white; color: plum; font-family: 'Roboto', sans-serif; } h1 { font-family: 'Roboto', sans-serif; font-size: 70px; margin: 0.2em; } h2 { margin: 1em 0 0.25em; } h2:first-of-type { margin-top: 0.5em; } table, tr, td { border: 2px solid black; } table { border-collapse: collapse; margin: 0 auto; color: black; } tr { height: 20px; } td { width: 20px; } input[type=number] { width: 6em; }
 <h1>Pixel Art Maker</h1> <h2>Choose Grid Size</h2> <form id="sizePicker" onsubmit="return false;"> Grid Height: <input type="number" id="inputHeight" name="height" min="1" value="1"> Grid Width: <input type="number" id="inputWidth" name="width" min="1" value="1"> <input type="submit" id="submit"> </form> <h2>Pick A Color</h2> <input type="color" id="colorPicker"> <h2>Design Canvas</h2> <table id="pixelCanvas"></table>

They key issue is that you were assigning the value of the inputs to the variables immediately instead of allowing the function to get the values.他们的关键问题是您立即将输入值分配给变量,而不是让 function 获取值。

  1. Coerce the strings from the values to Numbers, and use those in your loops.将字符串从值强制转换为数字,并在循环中使用它们。

  2. Remove the <form> element completely and change the <input type="submit" /> to a button with a type="button" attribute.完全删除<form>元素并将<input type="submit" />更改为具有type="button"属性的按钮。

  3. In fillCell use event.target to access the element that was clicked on.fillCell使用event.target访问被点击的元素。

 var table = document.getElementById("pixelCanvas"); var height = document.getElementById("inputHeight"); var width = document.getElementById("inputWidth"); var color = document.getElementById("colorPicker"); var submit = document.getElementById("submit"); var size = document.getElementById("sizePicker"); submit.addEventListener("click", makeGrid); function makeGrid() { table.innerHTML = ""; for (let x = 0; x < Number(height.value); x++) { var tr = table.insertRow(x); for (let y = 0; y < Number(width.value); y++) { var td = tr.insertCell(y); table.appendChild(tr); td.addEventListener("click", fillCell); tr.appendChild(td); } table.appendChild(tr); } } function fillCell(event) { event.target.style.backgroundColor = color.value; }
 body{text-align:center;background-color:#fff;color:plum;font-family:Roboto,sans-serif}h1{font-family:Roboto,sans-serif;font-size:70px;margin:.2em}h2{margin:1em 0.25em}h2:first-of-type{margin-top:.5em}table,td,tr{border:2px solid #000}table{border-collapse:collapse;margin:0 auto;color:#000}tr{height:20px}td{width:20px}input[type=number]{width:6em}
 <h2>Choose Grid Size</h2> Grid Height: <input type="number" id="inputHeight" name="height" min="1" value="1"> Grid Width: <input type="number" id="inputWidth" name="width" min="1" value="1"> <button type="button" id="submit">Submit</button> <h2>Pick A Color</h2> <input type="color" id="colorPicker"> <h2>Design Canvas</h2> <table id="pixelCanvas"></table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM