gpt4 book ai didi

javascript - 基于html属性的动态jquery选择器

转载 作者:行者123 更新时间:2023-11-28 18:38:05 25 4
gpt4 key购买 nike

我有以下代码:

<table id="Product">
<thead>
<tr>
<th>ProductId</th>
<th>Productname</th>
<th>Quantity</th>
<th>UnitPrice</th>
</tr>
</thead>
<tbody>

</tbody>
</table>

<button id="add" data-table="Product" data-url="Product/Add"></button>

然后在 JavaScript 文件中:

     $('#add').click(function () {
var url = $(this).attr("data-url");
var table = $(this).attr("data-table");
var tableBody = ???;
//some logic with tablebody

});

如何获取表格的表格主体?

最佳答案

$('#add').click(function () {
var $this = $(this);
var url = $this.data("url");
var tableId = $this.data("table");
var $tableBody = $("#" + tableId + " tbody");
});

了解有关 jQuery 选择器的更多信息,请访问 https://api.jquery.com/category/selectors/

这里我使用 ID SelectorDescendant Selector .

关于javascript - 基于html属性的动态jquery选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36636310/

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