简体   繁体   English

使用JQuery .insertBefore()方法未按给定顺序执行

[英]Using JQuery .insertBefore() Method isn't Executing in the Order Given

I have a problem where the matching end tag is migrating up to it's pair, instead of wrapping content as in the order I have listed the statements. 我有一个问题,匹配的结束标记要迁移到它的配对,而不是按照列出语句的顺序包装内容。 I am missing how/why this would happen. 我想念这将/为什么发生。 The JS Function: JS函数:

$('.add-contributor-button').click(function(){
 $("<input type=\"text\" name=\"contributor\">").insertBefore('.add-contributor-button');
 $("<select>").insertBefore('.add-contributor-button');
  $("<option value=\"\" name=\"author\">Author</option>").insertBefore('.add-contributor-button');
  $("<option value=\"\" name=\"editor\">Editor</option>").insertBefore('.add-contributor-button');
  $("<option value=\"\" name=\"illustrator\">Illustrator</option>").insertBefore('.add-contributor-button');
  $("<option value=\"\" name=\"translator\">Translator</option>").insertBefore('.add-contributor-button');
  $("<option value=\"\" name=\"other\">Other</option>").insertBefore('.add-contributor-button');
$("</select>").insertBefore('.add-contributor-button');
});

The HTML before(and what is the target for duplication.): 之前的HTML(以及复制的目标是什么):

<input type="text" name="contributor">
<select>
 <option value="" name="author">Author</option>
 <option value="" name="editor">Editor</option>
 <option value="" name="illustrator">Illustrator</option>
 <option value="" name="translator">Translator</option>
 <option value="" name="other">Other</option>
</select>
<div class="add-contributor-button">+ Add Contributor</div>

The Resulting HTML once the div is clicked(I am getting this from Firefox 50.1.0 inspector tool): 单击div后的结果HTML(我是从Firefox 50.1.0检查器工具中获得的):

<input type="text" name="contributor">
<select>
 <option value="" name="author">Author</option>
 <option value="" name="illustrator">Illustrator</option>
 <option value="" name="translator">Translator</option>
 <option value="" name="other">Other</option>
</select>
<input type="text" name="contributor">
<select></select>
 <option value="" name="author">Author</option>
 <option value="" name="illustrator">Illustrator</option>
 <option value="" name="translator">Translator</option>
 <option value="" name="other">Other</option>
<div class="add-contributor-button">+ Add Contributor</div>

I believe the issue here is a misunderstanding of how insertBefore() works in jQuery. 我相信这里的问题是对insertBefore()在jQuery中的工作方式的误解。

insertBefore() is not used to build lines of HTML. insertBefore()不用于构建HTML行。 If you specify an HTML element like $("<select>").insertBefore() , it will create a new <select> element and add it to your page, closing tags and all. 如果指定$("<select>").insertBefore()类的HTML元素,它将创建一个新的<select>元素并将其添加到您的页面中,并关闭标签和所有元素。

That being said, we can keep that code. 话虽如此,我们可以保留该代码。 We do want a new <select> element, after all. 毕竟,我们确实需要一个新的<select>元素。 But rather than inserting all our options, we want to append them, which adds them as children to the targeted element. 但是,我们不想插入所有选项,而是要附加它们,这会将它们作为子元素添加到目标元素。

Like so: 像这样:

$('.add-contributor-button').click(function() {
    $("<input type=\"text\" name=\"contributor\">").insertBefore('.add-contributor-button');
    $("<select>").insertBefore('.add-contributor-button')
        .append("<option value=\"\" name=\"author\">Author</option>")
        .append("<option value=\"\" name=\"editor\">Editor</option>")
        .append("<option value=\"\" name=\"illustrator\">Illustrator</option>")
        .append("<option value=\"\" name=\"translator\">Translator</option>")
        .append("<option value=\"\" name=\"other\">Other</option>");
});

I should really note, there are some more efficient ways of doing this. 我真的应该注意到,有一些更有效的方法可以做到这一点。 I was in the process of typing one, but zer00ne has posted a good example of how you can take the original inputs, and use the button to simply duplicate them instead of creating new ones from scratch each time. 我正在输入一个,但是zer00ne发布了一个很好的示例 ,说明了如何获取原始输入,并使用该按钮简单地复制它们,而不是每次都从头开始创建新输入。

Save yourself some headache and wrap <input> and <select> in a container (I used a <fieldset> ) and then .clone() it. .clone()一些麻烦,将<input><select>包裹在一个容器中(我使用了<fieldset> ),然后.clone()

SNIPPET SNIPPET

 $('.add-contributor-button').click(function() { var clone = $('.original').clone(true, true); clone.removeClass('original').addClass('clone').insertBefore('.add-contributor-button'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset class='original set'> <input type="text" name="contributor"> <select> <option value="" name="author">Author</option> <option value="" name="editor">Editor</option> <option value="" name="illustrator">Illustrator</option> <option value="" name="translator">Translator</option> <option value="" name="other">Other</option> </select> </fieldset> <div class="add-contributor-button">+ Add Contributor</div> 

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

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