Jquery帮助追加HTML不工作

Jquery help append HTML not working

本文关键字:工作 HTML 追加 帮助 Jquery      更新时间:2023-09-26

我试图添加这个HTML也当一个输入字段被添加:

<div id="redDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:red;"></div>
<div id="greenDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:green;"></div>

我未编辑的Jquery http://jsfiddle.net/gDChA/23/:

function findLastInput ( element ) {
  return $( element ).parent().prev().find('input').last();
}
    $('button.add').click ( function(e) {
        $(this).parent().find('button.remove').show();
        $(this).hide();
        var element = findLastInput(this).clone();
        var name = element.prop('name');
        var pattern = new RegExp(/'[(.*?)']/);
        var info = name.match(pattern)[1];
        element.prop({
          'value': '',
          'name' : name.replace(pattern, '[' + info + 'info' + ']'),
          'id'   : element.prop('id') + 'info',
          'type' : 'input',
          'class' : 'infoinput'
        });    
        $(this).parent().append(element);
    })
    $('button.remove').click ( function(e) {
        $(this).parent().find('button.add').show();
        $(this).hide();
        //findLastInput(this).remove('input');
        $(this).parent().find("input").remove();
    });

首先,我试着添加额外的HTML没有运气。我编辑了这个$(this).parent().append(element);

$(this).parent().append(element + '<div id="redDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:red;"></div>
    <div id="greenDiv" style="width:30px;height:30px;margin-top:10px;display:block;background:green;"></div>');

我想添加HTML并删除它,就像输入字段一样。

您可能需要将额外的html添加到element.html()中,而不是element (element只是从findLastInput函数返回的对象),因此它应该是这样的:

(美元).parent () .append ( element.html () + '');