繁体   English   中英

对表的每一行使用相同的图像作为链接

[英]Use same image for link for each row of a table

在一个表中,我在每一行上都有一个删除链接。 我正在尝试添加相同的图像进行链接。 图像仅添加到第一行,而不添加到其余行。 这就是我所做的。

$(document).ready(function () {
    $('.isSelected').click(function () {

        var pdtname = $(this).attr('data-productname');

        if ($(this).prop("checked")) {

            $("#result").show();

            if ($(".isSelected:checked").length == 2) {
                $(".isSelected").not(":checked").prop("disabled",true); 
            }
            var table = $('<table></table>').addClass('tfoo').attr("data-productname", pdtname);
            var row = $('<tr></tr>').addClass('rfoo').text(pdtname);

            **var link = $("#imgLogo").wrap('<a href="javascript:void(0);"></a>').addClass('lfoo');**

            row.append(link);
            table.append(row);
            $('#result').append(table);

            $("#result .lfoo").on('click', function () {
                $('input[data-productname="' + $(this).parent().parent().parent().attr("data-productname") + '"]').prop("checked", false);
                $(this).parent().parent().parent().remove();
                 $(".isSelected").not(":checked").prop("disabled",false);
            });


        } else {
            $('table[data-productname="' + pdtname + '"]').remove();
             $(".isSelected").not(":checked").prop("disabled",false);
        }

    });
});

这是图像链接的行var link = $("#imgLogo").wrap('<a href="javascript:void(0);"></a>').addClass('lfoo');

链接到jsfiddle

看起来您想要这样的东西: http : //jsfiddle.net/7kk2b5sp/1/

有更好的方法可以改善此问题,但这是另一个问题。 对于您所关心的问题,您想克隆图像并使用class而不是id

变化:

  • 链接创建js
  • img html中的属性和属性值
  • 添加了一些CSS

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM