Javascript 克隆时使用jQuery删除第一个类

Javascript 克隆时使用jQuery删除第一个类,javascript,jquery,html,Javascript,Jquery,Html,我编写了一个简单的函数来克隆字段: 在线演示: HTML: 我想从第一次输入中删除删除图标,我尝试了: $('p.child .icon-delete:first').css('display','none') 但并非所有输入都显示删除图标 PS:如果我能优化上面的代码,我会很高兴:)我想这会达到目的。$('p.child.icon delete:first')。css('display','none')隐藏了所有。icon delete是p.child的子对象。在您的例子中,allp.chi

我编写了一个简单的函数来克隆字段:

在线演示:

HTML: 我想从第一次输入中删除删除图标,我尝试了:

$('p.child .icon-delete:first').css('display','none')
但并非所有输入都显示删除图标


PS:如果我能优化上面的代码,我会很高兴:)

我想这会达到目的。
$('p.child.icon delete:first')。css('display','none')
隐藏了所有
。icon delete
p.child
的子对象。在您的例子中,all
p.child
的子项。icon delete

$('p.child:first .icon-delete').css('display','none')

看看这个:

// Keep a single clone of the original
var clonedField = $('.child').clone(),
    main = $('#main');

// Add in the delete <a> to the cloned field
$('<a>', {
    text: 'delete',
    class: 'icon-delete',
    href: '#',
    click: function(){
        $(this).parent().remove();
        return false;
    }
}).appendTo(clonedField);

// Clone the cloned original and append it back to the list
$('#add-input').click(function() {
    main.append(clonedField.clone());
    return false;
});
//保留原始文件的单个克隆
var clonedField=$('.child').clone(),
main=$(“#main”);
//在删除中添加

  • 演示:
$(函数(){
$(“#添加输入”)。单击(函数(){
$(“”+
“

”)。附录(“#main”); }); //只是为了。。。 $('.icon delete').live('单击', 函数(){ $(this).parent().fadeOut(500, 函数(){ $(this.remove(); }); }); });
gotchas:

  • 你为什么要克隆
  • 为什么要将删除按钮放在第一位

  • 当您调用此$('p.child.icon delete:first').css('display','none')?结果相同,在开始和结束时,您可以添加HTML,但您需要使用标记,以便为您转义HTML。使用编辑器上的“代码”按钮(即
    {}
    )或
    ctrl
    +
    K
    为您执行此操作。当您调用此$('p.child.icon delete:first').css('display','none')时我想是因为克隆的缘故,我想先克隆没有.delete图标的.child…然后把它添加到克隆的对象中。我不想写太多的行:(我想这将是相同的代码行数。)因为你必须从一个地方删除一行,然后将行粘贴到另一个地方…任何方式都由你决定。祝你好运
    $('p.child:first .icon-delete').css('display','none')
    
    $('#add-input').bind('click',function ()
    {
        var newField = $('.child').clone();
        newField.toggle().attr('class', '');
        registerRemoveHandlers(newField, '.icon-delete');
        $('p.child:last').after(newField); //append after not cloned child
    
        newField.parent().children('p').find('a').show(); //show all 'delete' label
        newField.prev().find('a').hide(); //hide label from the first child which is right the one before our clone
    
        return false;
    });
    
    // Keep a single clone of the original
    var clonedField = $('.child').clone(),
        main = $('#main');
    
    // Add in the delete <a> to the cloned field
    $('<a>', {
        text: 'delete',
        class: 'icon-delete',
        href: '#',
        click: function(){
            $(this).parent().remove();
            return false;
        }
    }).appendTo(clonedField);
    
    // Clone the cloned original and append it back to the list
    $('#add-input').click(function() {
        main.append(clonedField.clone());
        return false;
    });
    
    $(function() {
        $('#add-input').click(function() {
            $('<p><input type="text" name="user[]" /> ' + 
              '<a href="#" class="icon-delete">delete</a></p>').appendTo('#main');
        });
    
        // just for sake...
        $('.icon-delete').live('click',
        function() {
            $(this).parent().fadeOut(500,
            function() {
                $(this).remove();
            });
        });
    });