gpt4 book ai didi

javascript - 选定的维度转换为 div 表

转载 作者:行者123 更新时间:2023-11-28 00:47:06 25 4
gpt4 key购买 nike

谁能帮我逆向工程thisthis页?用户通过网格系统选择维度的地方。然后将所选框的数量转换为相同尺寸的 div。

或者有没有人推荐 JavaScript,如果有的话是哪一个?

最佳答案

我们这里有两个问题,一个是选择,一个是生成。生成很简单:

注意:这样做既快速又肮脏;不应该使用 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 = "&nbsp;"
// 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>&amp;nbsp;</")
.replace(/</gi, "&lt;")
.replace(/>/gi, ">\n");
}

return table;
}

selectionContainer.appendChild(generateDiv(10, 10, true, targetContainer, textContainer));

</script>

并且类似地,通过向每个单元格添加行和列信息的类似函数来制作可选表格。然后,表上的 mouseClick 事件将读取行和列并传递给生成代码。

关于javascript - 选定的维度转换为 div 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53462807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com