简体   繁体   English

从.html()中排除元素

[英]Exclude Element from .html()

I have to remove a specific element (button with #add_phone) from .html() of jquery. 我必须从jquery的.html()中删除特定元素(带有#add_phone的按钮)。

So here's the thing. 这就是问题。 At first there are field(phone number) + select(phone type) + button(#add_phone), and all three are enclosed in div as a container. 首先是字段(电话号码)+选择(电话类型)+按钮(#add_phone),所有这三个字段都作为容器包含在div中。 And when I click the button, it will recreate that through .html(). 当我单击按钮时,它将通过.html()重新创建。 The JS is as follows: JS如下:

$('#add_phone').click(function() {
    $('div.multiple_number:last').after('<div id="phone_div_id' + phone_div_id + '" class="multiple_number">'+ $('div.multiple_number').html() +'</div>');
    ...
    //append a remove [-] button, etc...     
});

and here's the html: 这是html:

<div class="multiple_number" id="phone_div_id0">
    <label>Phone Number(s):</label>
    <input name="phone" id="phone[]" placeholder="Phone Number"/>
    <select name="phone_type[]" id="phone_type">
        <option value="1">Mobile</option>
        <option value="2">Home</option>
        <option Value="3">Office</option>
        <option Value="3">Fax</option>          
    </select>
    <input type="button" name="add_phone" class="add_phone_class" id="add_phone" value="ADD MORE" />
</div>

So in effect, I am creating multiple phone numbers for a form. 因此,实际上,我正在为表单创建多个电话号码。 But, here's the problem. 但是,这就是问题所在。 Inside is an input type="button" ( #add_phone button). 里面是一个输入type =“ button”( #add_phone按钮)。 And I would want to exclude it from .html(). 我想将其从.html()中排除。

I have tried: 我努力了:

$('div.multiple_number:not(#add_phone)').html()
$('div.multiple_number:not(input#add_phone)').html()
$('div.multiple_number:not(#add_phone)').not(#add_phone).html()
$('div.multiple_number:not(#add_phone)').not(input#add_phone).html()

And the class name counterpart instead of using id name. 与类名对应,而不是使用id名称。 I wouldn't also want to place the #add_phone button outside the div, for aesthetics reason. 出于美观原因,我也不想将#add_phone按钮放置在div之外。

I'm a little bit unclear about what you're looking for, but I assume that when the #add_phone button is clicked, you want the form to be duplicated and added below it with the exception of the #add_phone button itself. 对于您要查找的内容,我有点不清楚,但是我假设单击#add_phone按钮时,除了#add_phone按钮本身之外,您希望复制表格并将其添加到表格下方。

Working off that assumption, the following should work 按照这个假设,以下应该起作用

$('#add_phone').click(function() {
    var numberForms = $('div.multiple_number');
    var newNumberForm = numberForms.eq(0).clone(true);
    newNumberForm.find('#add_phone').remove();
    newNumberForm.attr('id', 'phone_div_id' + numberForms.length);
    numberForms.last().after(newNumberForm);
});

Here's a live jsfiddle demo to show it working. 这是一个演示jsfiddle实时演示

Your initial attempts didn't work for a few reasons. 您的最初尝试由于以下几个原因而无效。 The main one being that :not() selector and .not () methods only operate on the element being selected. 主要的原因是:not()选择器和.not ()方法仅对所选元素起作用。 It doesn't filter based on child elements. 它不会基于子元素进行过滤。 Those methods would only work if the element you were selecting <div class="multiple_number" /> also had the ID add_phone . 仅当您选择<div class="multiple_number" />的元素也具有ID add_phone这些方法才有效。

Also, it is not recommended to use .html() as a way of cloning methods. 另外,不建议使用.html()作为克隆方法。 Using string manipulation as an alternative to direct DOM manipulation can cause problems later on. 使用字符串操作代替直接DOM操作可能会在以后引起问题。 Using .html() will force you to have to re-bind event handlers to the newly created DOM elements. 使用.html()将迫使您必须将事件处理程序重新绑定到新创建的DOM元素。 The strategy I've provided above should be more future-proof, since it will also clone event handlers for any elements being copied. 我上面提供的策略应该更具前瞻性,因为它还会为所有要复制的元素克隆事件处理程序。 There are also cases where certain browsers will not replicate the original elements exactly when calling .html() , which is another reason to avoid it unless you have a specific reason for serializing your DOM elements as a string. 在某些情况下,某些浏览器在调用.html()时不会完全复制原始元素,这是避免使用它的另一个原因,除非您有特定的原因将DOM元素序列化为字符串。

Try this instead : 试试这个代替:

var innerHTML = $("div.multiple_number").html()
                .replace($("div.multiple_number input#add_phone").html(), "");

Good Luck 祝好运

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

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