gpt4 book ai didi

javascript - jQuery 点击一个动态添加的元素

转载 作者:行者123 更新时间:2023-11-28 02:51:31 25 4
gpt4 key购买 nike

我使用 jQuery 创建了一个 HTML 表格,我想点击第二行中的第一个链接。生成的 HTML 看起来像这样

<tr><td>4</td><td>text</td></tr>
<tr id="xrow22" class="xrow"><td colspan="2"><a>0</a><a>1</a></td></tr>

当我使用鼠标单击该行时,处理点击的函数运行良好

$('table').on('click', '[id^=xrow] a', function(){ ...

当我尝试从另一个调用此函数时,它不起作用。我使用警报来查看一些值,令人惊讶的是下面的代码警报(第二个值应该是 0)

<a>0</a><a>1</a>,,[object Object]

代码:

$('table').on('click', 'tr:not(.xrow)', function(e){
$(this).next().children('td:first-child a:first-child').click();
alert($(this).next().children('td:first-child').html() +','+ $(this).next().children('td:first-child a:first-child').text() +','+ $(this).next().children('td:first-child a:first-child'));
});

最佳答案

您可以使用 $().live() 函数代替 $().on() 函数。基本上,当您创建一个新元素时,jquery 不会重新运行,甚至不会将点击附加到它。 http://api.jquery.com/live/

关于javascript - jQuery 点击一个动态添加的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39183585/

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