我有一个非常奇怪的情况 - 我可以将图片添加到<td>
中的所有<tr>
代码,但不能仅添加到特定的<td>
,它会将图片添加到错误的位置,仅列中的最后一项
var img = new Image();
img.src = 'https://kaymo75663.i.lithium.com/html/rank_icons/Icon-16x16TeamViewer-2016.png';
$(document).ready(function() {
$('tbody.gridcontents_ticketmanagegrid_parent tr').each(function() {
//console.log($(this).find('td:nth-child(4)').html()); --> prints right html, where I need to add image
// console.log($(this).find('td:eq(3)').html()); --> also prints right html, where I need to add image
// $(this).find("td").append(img); --> work fine, add image to every cell
$(this).find('td:eq(3)').append(img); // --> doesn't work for some unknown reason!
$(this).find('td:nth-child(4)').append(img); // --> doesn't work for some unknown reason!
});
});
我尝试使用jquery 1.11.0和2.1.0,结果相同:(
请帮我解决问题
这是一个示例小提琴https://jsfiddle.net/serikov/brxkq7o6/,在这里我想将团队查看器图标添加到姓氏列中的所有项目
答案 0 :(得分:0)
请修改$('table tr')
至$('table')
,然后使用tr
查找每个td
。见下面的小提琴,
https://jsfiddle.net/saifudazzlings/brxkq7o6/4/
答案 1 :(得分:0)
问题已解决,解决方案很简单 - 使用.append($.clone(img))
代替.append(img)
。好像我们不能多次向DOM添加相同的元素
答案 2 :(得分:0)
请试试这个:
$( document ).ready(function() {
var img = new Image();
img.src = 'https://kaymo75663.i.lithium.com/html/rank_icons/Icon-16x16TeamViewer-2016.png';
$('table').each( function(){
$(this).find('tr td:nth-child(3)').append(img);
});
});