网格视图表格(Grid View Table)通常是指以行列形式展示数据的HTML表格,可能通过CSS样式呈现网格状外观。jQuery是一个快速、简洁的JavaScript库,可以简化HTML文档遍历、事件处理、动画和Ajax交互。
// 通过ID选择器更改特定单元格
$('#cellId').text('新文本');
// 通过类选择器更改多个单元格
$('.cellClass').text('新文本');
// 通过行列索引更改特定单元格
$('table tr:eq(2) td:eq(1)').text('新文本'); // 第3行第2列
// 遍历所有行
$('table tr').each(function() {
// 遍历当前行的所有单元格
$(this).find('td').each(function() {
// 根据条件更改文本
if ($(this).text() === '旧文本') {
$(this).text('新文本');
}
});
});
// 更改特定列中满足条件的单元格
$('table tr td:nth-child(2)').filter(function() {
return $(this).text().indexOf('特定内容') !== -1;
}).text('新文本');
// 如果需要包含HTML标签
$('#cellId').html('<strong>新文本</strong>');
// 点击单元格时更改其文本
$('table td').click(function() {
$(this).text('新文本');
});
<table id="myTable" border="1">
<tr>
<td>产品A</td>
<td class="price">100</td>
</tr>
<tr>
<td>产品B</td>
<td class="price">200</td>
</tr>
</table>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// 更改所有价格单元格
$('.price').text(function(index, oldText) {
return '$' + oldText;
});
// 点击行时更改产品名称
$('#myTable tr').click(function() {
$(this).find('td:first').text('新产品');
});
});
</script>
这个示例展示了如何批量格式化价格文本以及如何通过点击事件动态更改产品名称。
没有搜到相关的文章