在表格单元格内替换图像不起作用
Replacing image inside a table cell doesn’t work
我有一个表,其中使用jQuery动态添加行(使用prepend在顶部的最新行)。如果是errorLine,则第一列中的文本将为"x"。如果它是一个successLine,第一列的文本将是数字,如1,2,3…
如果最新的行是errorLine,我需要应用背景颜色(使用类"firststerrorline"),并将文本"x"替换为图像。同时,我需要从所有其他行删除图像,并将图像替换为文本"x"。我尝试了多种jQuery方法,但都不起作用。以下是失败的方法之一。
//$("#tblVisualAidResult tbody tr td:first img").hide().parents("tr td:first").html('x');
有什么想法我们可以做到这一点吗?
Demo - jsFiddle
jQuery代码
$(document).ready(function ()
{
function styleFirstErrorLine(isFailureExist)
{
$("#tblVisualAidResult tbody tr").removeClass('firstErrorLine');
//$("#tblVisualAidResult tbody tr td:first img").hide().parents("tr td:first").html('x');
if(isFailureExist)
{
$("#tblVisualAidResult tbody tr:first").addClass('firstErrorLine');
var firstCell = $("#tblVisualAidResult tbody tr:first td:first");
$(firstCell).html('<img width="50" height="50" class="errorImage" src="../images/Error2_BMP.JPG"/>');
}
}
styleFirstErrorLine(true);
});