将列上的链接链接到表中的另一列

时间:2013-07-22 10:08:52

标签: jquery html-table jquery-clone

我想在td之后创建link imagetd:nth-child(2)并克隆链接a.link_topic_title

我已制作此脚本,但链接仅在第一行克隆链接

$(document).ready(function () {
    $('#preview_active').change(function () {
        if ($(this).is(':checked')) {
            var url_thread = $('.link_topic_title').attr("href");
            var url_tooltip = "http://example.com" + url_thread
            $(".zebra thead tr th:first-child").attr('colspan', 4);
            $('.zebra tbody tr td:nth-child(2)').after('<td class="span1 icon"><a href="' + url_thread + '"><img src="http://icons.iconarchive.com/icons/visualpharm/must-have/256/Preview-icon.png" height="30px" width="25px" rel="tooltip" data-original-title="' + url_tooltip + '"/></a></td>');
            $('.zebra tbody tr td:nth-child(3) a img[rel=tooltip]').tooltip();
        } else {
            $(".zebra thead tr th:first-child").attr('colspan', 3);
            $('.zebra tbody tr td:nth-child(3)').remove();
        }
    });
});

jsfiddle:http://jsfiddle.net/5Vpra/2/

如何将列上的列表链接克隆到另一列(创建td)?

更新

我尝试过使用每个,我设法获取所有链接,但没有成功克隆到每个td,

$(document).ready(function () {
    $('#preview_active').change(function () {
        if ($(this).is(':checked')) {

            classes = {};
               $('.link_topic_title').each(function() {
               $($(this).attr("href").split(' ')).each(function() { 
               if (this !== '') {
                 classes[this] = this;
                }    
                });
               });

            tds = '';
            $(".zebra thead tr th:first-child").attr('colspan', 4);
            $('.zebra tbody tr td:nth-child(2)').after('<td class="span1 icon"></td>');

            for (class_name in classes) {
             var url_tooltip = "http://example.com" + class_name
             tds += $('.zebra tbody tr td:nth-child(3)').append('<a href="' + class_name + '"><img src="http://icons.iconarchive.com/icons/visualpharm/must-have/256/Preview-icon.png" height="30px" width="25px" rel="tooltip" data-original-title="' + url_tooltip + '"/></a>');
                 $('.zebra tbody tr td:nth-child(3) a img[rel=tooltip]').tooltip();
          };
        } else {
            $(".zebra thead tr th:first-child").attr('colspan', 3);
            $('.zebra tbody tr td:nth-child(3)').remove();
        }
    });
});

jsfiddle:http://jsfiddle.net/5Vpra/3/

2 个答案:

答案 0 :(得分:0)

var url_thread = $('.link_topic_title').attr("href");

此行从找到的第一个.link_topic_title元素获取attr href。这就是为什么您创建的所有链接都具有相同的URL。我认为你需要一个for循环,以便在每次添加链接时知道你在哪一行。但我不是专家,也许有更顺畅的方法来做到这一点。

答案 1 :(得分:0)

解决

我尝试在index上添加for loop作为tr的标识符。

$(document).ready(function () {
    $('#preview_active').change(function () {
        if ($(this).is(':checked')) {

            var classes = {};
               $('.link_topic_title').each(function() {
               $($(this).attr("href").split(' ')).each(function() { 
               if (this !== '') {
                 classes[this] = this;
                }    
                });
               });

            tds = '';
            $(".zebra thead tr th:first-child").attr('colspan', 4);
            $('.zebra tbody tr td:nth-child(2)').after('<td class="span1 icon"></td>');
            var i = 0;
            for (class_name in classes) {
             i = i+1;
             var url_tooltip = "http://domain.com" + class_name
             tds +=  $('.zebra tbody tr:nth-child(' + i + ') td:nth-child(3)').append('<a href="' + class_name + '"><img src="http://icons.iconarchive.com/icons/visualpharm/must-have/256/Preview-icon.png" height="30px" width="25px" rel="tooltip" data-original-title="' + url_tooltip + '"/></a>');
                 $('.zebra tbody tr td:nth-child(3) a img[rel=tooltip]').tooltip();
          };
        } else {
            $(".zebra thead tr th:first-child").attr('colspan', 3);
            $('.zebra tbody tr td:nth-child(3)').remove();
        }
    });
});

$('.zebra tbody tr:nth-child(' + i + ') td:nth-child(3)')结果:

$('.zebra tbody tr:nth-child(1) td:nth-child(3)')

$('.zebra tbody tr:nth-child(2) td:nth-child(3)')

...

...

jsfiddle:http://jsfiddle.net/5Vpra/4/