gpt4 book ai didi

jquery - 将部分代码添加到jquery中的td中

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

我在使用 jQuery 将一些 HTML 代码添加到现有 tabletd 元素中时遇到了一些问题。我的 HTML 代码是:

<table>
<tbody>
<tr>
<td>Something</td>
<td><strong>10</strong></td>
</tr>
</table>
</tbody>

现在我编写了这段 jQuery 代码,在包含“Something”的单元格中添加工具提示:

$('table tr').each(function(){
if ($(this).find('td').text() == 'Something') {
$(this).html('<td><a class="tooltips" title="bla bla bla bla">Something</a></td>')
}
});

在这种情况下,jQuery 函数可以正常工作,我已在 td 内容中添加了工具提示,但第二个 td 元素被删除。我尝试过使用 .find('td:first') 但无论如何,第二个 td 消失了。

有人可以告诉我如何正确编写 jQuery 代码,以便第二个 td 保留在页面中吗?非常感谢您的帮助。

最佳答案

问题是因为您覆盖了 tr 元素中的所有 HTML,而不仅仅是替换包含 Something 文本的第一个 td值(value)。试试这个:

$('table td').each(function(){
if ($(this).text() == 'Something'){
$(this).replaceWith('<td><a class="tooltips" title="bla bla bla bla">Something</a></td>');
}
});

关于jquery - 将部分代码添加到jquery中的td中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511940/

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