0
    $('#addmore').click(function() {
        var row = $('.crossref:last').clone();
        $('.crossref:last').after(row);
    });

Above code is used to clone some HTML & paste the cloned HTML right after the last cloned row. I have a lot of addmore buttons with the same functionality. I'm really concerned with this code snippet.

I'm wondering is there a way to optimize this code?

4

2 回答 2

0

您可以将clone()and after()in 合并为一行,这也将删除重复的.crossref:last选择器之一:

$('#addmore').click(function() {
    $('.crossref:last').after(function() { $(this).clone(); });
});
于 2013-10-11T09:52:18.583 回答
0

甚至更好

$('#addmore').click(function() {
    var lastEl = $('.crossref:last');
    lastEl.after(lastEl.clone());
});
于 2013-10-11T09:54:01.240 回答