javascript - 如何更改Jquery克隆位置?

标签 javascript jquery html

当点击加号图标时,我创建了一个动态克隆 html。它工作正常,但问题是当我单击加号按钮克隆位置在加号按钮之后,但我想更改加号图标之前的克隆位置。

这里是 HTML 代码

<div class="table-responsive" style="max-height: 200px;">                                                                    
   <table class="" style="width: 100%;">
        <tbody id="attendCourse">                                                                        
             <tr id="attendCourse0">                                                                    
               <td style="width: 85%;">                                                  
                  <input name="course" class="form-control" type="text">                                                                                        
               </td>
               <td>
                 <img id="addAttand" src="<?php echo ASSETS_URL; ?>admin/images/plus1.png">
               </td>                                                                 
             </tr>

        </tbody>
    </table>
</div>

HTML 设计图像

enter image description here

Jquery 代码

$(function() {
var $componentTB = $("#attendCourse"),
    $firstTRCopy = $("#attendCourse0").clone();
    $idVal = 1;

$("#addAttand").click(function() {
    var copy = $firstTRCopy.clone();         
    var newId = 'attendCourse' +$idVal;
    copy.attr('id', newId);
    $idVal += 1;
    copy.children('td').last().replaceWith("<td><div class=''><img onclick=\"remove('" + newId + "')\" src="+JS_SITEURL+"assets/admin/images/negative.png></div></td>");        
    $componentTB.append(copy);
});
});

最佳答案

使用insertbefore jquery的方法。

copy.insertBefore($("#attendCourse0"));

关于javascript - 如何更改Jquery克隆位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52328848/

相关文章:

javascript - 我怎样才能得到一个普通类div的内容

jquery - 更改 jQuery fullcalendar 中每个项目的颜色

html - 如何使用 css 隐藏列表并在鼠标位于上方时显示它?

javascript - 在 React 钩子(Hook)中的另一个卸载 useEffect 中,状态的改变不会受到影响

javascript - addEventListener() 方法 id 在 IE 11.0.12 中不起作用,似乎在 11.0.10 中起作用

html - 元素特定的 CSS 选择器不好吗?

html - 在 Elasticsearch 中存储 HTML 文档

javascript - 防止 Safari 在单击按钮时丢失正文中的选定文本

javascript - 如果图像较大,则在窗口中调整 Lightbox 容器

javascript - 如何以屏幕阅读器仍会阅读文本并向用户指示它已被禁用的方式将单选按钮设置为 "disable"?