javascript - 'click' 上的事件不适用于链接图像

标签 javascript php jquery html css

我尝试将 VB6 应用程序迁移到基于 Web 的应用程序。我停留在无法进一步排除故障的某个位置。 我想做的是在类“confirmation1”上实现点击事件 - 链接(黑色十字图像)。 我按以下方式构建表格行:

var tr_str = "<tr>" +
"<td align='left'>" + result[i].produkt + "</td>" +
"<td align='right'>" + result[i].waga + "</td>" +
"<td align='right'>" + Math.round(result[i].kalorie) + "</td>" +
"<td align='right'>" + (Math.round(result[i].bialko * 100) / 100).toFixed(2) + "</td>" +
"<td align='right'>" + (Math.round(result[i].tluszcze * 100) / 100).toFixed(2) + "</td>" +
"<td align='right'>" + (Math.round(result[i].weglowodany * 100) / 100).toFixed(2) + "</td>" +
"<td align='center'>" +
"<a href='test' title='Usuń' class='confirmation1'><img src='pictures/cross16.jpg' width='10' height='10' style='margin: 0px 0px' /></a>" ;
"</td>" +
"</tr>";
$("#TabelaDetali table").append(tr_str);

然后我执行以下操作:

$('.confirmation1').on('click', function(e) {
    e.preventDefault();
    var lnk = $(this).attr('href');
    var product_name = $(this).parents("tr").find("td").first().html();
    console.log(product_name);
    });

我不知道为什么我看不到这个工作。当我将光标移到表格右侧的十字图像上时(screenshot1 - 表格第 1 行和第 2 行,第 7 列)或单击它 - 没有任何反应。 screenshot1

我知道隐藏了很大一部分代码,但我不知道如何在此处附加文件。 任何想法如何解决它?


更新1

我在这里复制了代码文件:https://www.dropbox.com/s/74bso1ux6noywhy/html.zip?dl=0

最佳答案

既然你已经使用 jQuery 附加了这个 html,那么你应该像这样使用 ON 事件:

$('body').on('click', '.confirmation1', function(e) {
    e.preventDefault();
    var lnk = $(this).attr('href');
    var product_name = $(this).parents("tr").find("td").first().html();
    console.log(product_name);
});

关于javascript - 'click' 上的事件不适用于链接图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239158/

相关文章:

javascript - 什么工具生成 JavaScript?

javascript - 如何调整一个div相对于其他div的高度

PHP - 使用函数编写 SQL 查询

jquery - Bootstrap : Modal shows even when button disabled

javascript - jquery加载div属性返回未定义

javascript - 如何将字符串拆分为 n 个字符的段?

php - 更新表 php-mysql 中的值

php - file_exists() 用于文件名中包含 (&) 的文件

javascript - Jquery - 获取最接近类的属性值

javascript - 将我的 'IDs' 动态加载到我的 Backbone Collection 中?