


![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


у нас есть две проблемы: одна выбирает, а другая создает. Генерация проста:
Уведомление: Это делается быстро и грязно; innerHtml не следует использовать, напишите мне, если вы хотите, чтобы он был более отполированным :) https://jsfiddle.net/ynfb3Lh2/9/
<div id = "selection">
</div>
<div id = "target">
</div>
<pre id = "text">
</pre>
<script>
// Prepare our targets
const selectionContainer = document.getElementById("selection");
const targetContainer = document.getElementById("target");
const textContainer = document.getElementById("text");
// rows: How many rows to generate?
// cols: How many cols to generate?
// [_insertCallback]: should we insert callback?
// [_targetContainer]: where should we place created divs?
// [_textContainer]: sWhere should we write our HTML as plain text?
function generateDiv(rows, columns, _insertCallback, _targetContainer, _textContainer) {
// Create our wrapping elements
const table = document.createElement("table");
const tbody = document.createElement("tbody");
for (let r = 0; r < rows; ++r) {
// Each row is created here...
const tr = document.createElement("tr");
for (let c = 0; c < columns; ++c) {
const td = document.createElement("td");
td.text = " "
// if callback is defined, and we have a target - allow us to generate new table on each click
if (_insertCallback && _targetContainer) {
td.onclick = () => generateDiv(r + 1, c + 1, false, _targetContainer, _textContainer)
}
tr.appendChild(td)
}
// ... and added here
tbody.appendChild(tr)
}
table.appendChild(tbody)
if (_targetContainer && !_insertCallback ) {
_targetContainer.innerHTML = '';
_targetContainer.appendChild(table);
}
if (_textContainer && !_insertCallback ) {
_textContainer.innerHTML = table.outerHTML
.replace(/td><//gi, "td>&nbsp;</")
.replace(/</gi, "<")
.replace(/>/gi, ">\n");
}
return table;
}
selectionContainer.appendChild(generateDiv(10, 10, true, targetContainer, textContainer));
</script>
и аналогично создание таблицы с возможностью выбора выполняется с помощью аналогичной функции, которая добавляет информацию о строках и столбцах в каждую ячейку. Затем событие mouseClick в таблице прочитает строки и столбцы и перейдет к коду генерации.
Между прочим, если вы не заметили, существует еще одна ревизия: jsfiddle.net/ynfb3Lh2/9
Как мне создать функцию, которая добавляет строку и столбец, и как их выделить? Я новичок в этом, ты!