gpt4 book ai didi

javascript - JS无法读取JS生成的行?

转载 作者:行者123 更新时间:2023-11-28 15:16:46 25 4
gpt4 key购买 nike

在一个HTML文件中,我使用JS生成表格行来显示数据库返回的数据:

function appendResult(data) {
var result = JSON.parse(data);
var row = result.length;
if (row == 0) {
$("#productList").html("No matching result.");
} else {
$("#productList").html("");
var i = 0;
while (i < row) {
// For each return record, create a new row, then write data into cell accordingly. New records are always written to last cell.
$("#productList").append("<tr class='hightLight'><td class='sku'></td><td class='productName'></td><td class='description'></td><td class='qtyPerCtn'></td><td class='weight'></td><td class='upc'></td><td class='gtin'></td><td class='vendorCode'></td><td class='note'></td></tr>");
$("td.sku").last().html(result[i]["sku"]);
$("td.productName").last().html(result[i]["productName"]);
$("td.description").last().html(result[i]["description"]);
$("td.qtyPerCtn").last().html(result[i]["qtyPerCtn"]);
$("td.weight").last().html(result[i]["masterWeightLb"]);
$("td.upc").last().html(result[i]["UPC"]);
$("td.gtin").last().html(result[i]["gtin"]);
$("td.vendorCode").last().html(result[i]["vendorCode"]);
$("td.note").last().html(result[i]["note"]);
i++;
}
}
}

然后我有一个函数可以在鼠标滑过该行时突出显示该行:

// high light row when mouse roll over
$(document).ready(function () {
$(".hightLight").hover(function () {
$(this).addClass("highLightOnRollOver");
}, function () {
$(this).removeClass("highLightOnRollOver");
});
});

但显然这不起作用。突出显示功能不起作用。但如果我在纯 html 中放入一行,它就会起作用:

<table>
<tr class="hightLight"><td>test</td></tr>
</table>

这是否意味着JS函数无法识别JS生成的元素?我该如何解决这个问题?

最佳答案

即使您在 dom 准备好后添加元素,这也会起作用:

// high light row when mouse roll over
$(document).ready(function () {
$("table")
.on('mouseenter', ".hightLight", function () {
$(this).addClass("highLightOnRollOver");
})
.on('mouseleave', ".hightLight", function () {
$(this).removeClass("highLightOnRollOver");
});
});

关于javascript - JS无法读取JS生成的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33533703/

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