gpt4 book ai didi

javascript - 如何在 html 表格中启用新添加的行可点击?

转载 作者:行者123 更新时间:2023-12-01 02:01:24 26 4
gpt4 key购买 nike

我有一个表,每行都有一个类“.clickablerow”,并且我定义了 onclick 函数,当单击该行时,将显示一个对话框,然后我可以在一些文本上方或下方插入作为新行。问题是,虽然我已将“.clickablerow”添加到这些新添加的行中,但它们实际上不可单击,并且没有显示这样的对话框。

我的代码是这样的:

JS:

$(document).ready(function() {
$(".clickable-row").click(function(){

var i = $(this).rowIndex;
var html = "<tr class='clickable-row' > <td> a test </td></tr>";

$('#table_id > tbody > tr').eq(i).after(html);
});

HTML:

<head>
...
<link rel="stylesheet" href="../static/css/main.css">
<link rel="stylesheet" href="./static/css/bootstrap.min.css">
<script src="./static/js/jquery.min.js"></script>
<script src="./static/js/bootstrap.min.js"></script>
<script src="./static/js/jquery.expander.min.js" type="text/javascript"></script>
<script src="./static/js/main.js" type="text/javascript"></script>
....
<head>

<body>
....
<table>
<tbody>
<tr class='clickable-row' >
<td style="border: 0;"> Initial message ....</td>
</tr>
</tbody>
</table>
....
</body>

如果我单击该行,将添加一个类为“clickable-row”的新行,但是该新行不可单击。任何想法都欢迎!谢谢

最佳答案

delegate the click event<table><tbody> :

const tableBody = document.getElementById('tableBody');

tableBody.onclick = (e) => {
const target = e.target;

let row = target;

while (!row.classList.contains('clickable-row') && row !== tableBody) row = row.parentElement;

if (row === tableBody) {
return; // Ignore the click if we could not find a .clickable-row
}

const newRow = tableBody.insertRow(target.rowIndex);

newRow.className = Math.random() < 0.5 ? 'clickable-row' : 'disabled-row';
newRow.innerHTML = '<td>...</td>';
};
table {
font-family: monospace;
width: 100%;
border-collapse: collapse;
text-align: center;
user-select: none;
}

tr.disabled-row {
background: #EEE;;
}

tr.clickable-row {
cursor: pointer;
}

tr.clickable-row:hover {
background: yellow;
}

td {
border: 2px solid black;
padding: 8px 4px;
}
<table>
<tbody id="tableBody">
<tr class="clickable-row"><td>...</td></tr>
</tbody>
</table>

使用 jQuery,您可以使用 .on() :

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements:

$( "#dataTable tbody tr" ).on( "click", function() {
console.log( $( this ).text() );
});

An event-delegation approach attaches an event handler to only one element, the tbody, and the event only needs to bubble up one level (from the clicked tr to tbody):

$( "#dataTable tbody" ).on( "click", "tr", function() {
console.log( $( this ).text() );
});

前面的示例将如下所示:

$('#tableBody').on('click', '.clickable-row', (e) => {
$(e.currentTarget).after(`<tr class=${ Math.random() < 0.5 ? 'clickable-row' : 'disabled-row' }><td>...</td></tr>`);
});
table {
font-family: monospace;
width: 100%;
border-collapse: collapse;
text-align: center;
user-select: none;
}

tr.disabled-row {
background: #EEE;;
}

tr.clickable-row {
cursor: pointer;
}

tr.clickable-row:hover {
background: yellow;
}

td {
border: 2px solid black;
padding: 8px 4px;
}
<table>
<tbody id="tableBody">
<tr class="clickable-row"><td>...</td></tr>
</tbody>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于javascript - 如何在 html 表格中启用新添加的行可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538507/

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