简体   繁体   English

点击按钮增加Div

[英]Div Increment on button click

Need javascript for incrementation... 需要使用JavaScript进行递增...

I have two with class names, parent and child (child inside the parent). 我有两个班级名称,父级和子级(父级中的子级)。

Each has a button outside them. 每个按钮外面都有一个按钮。 If I click the button outside the parent, the div should be incremented with child inside it. 如果单击父级外部的按钮,则div应在其内部增加子级。

And if the button outside the child is clicked, the child div must be incremented. 并且,如果单击了孩子外部的按钮,则必须增加孩子div。

Find the below link as demo. 找到以下链接作为演示。 LINK 链接

HTML code is HTML代码是

<div class="parent">
     <div class="child">
     </div><br>
     <a href="#" class="inc-child">Add one more child</a>
</div><br>
<a href="#" class="inc-parent">Add one more Parent</a>

You can use a click handlers with event delegation and cloning like below 您可以将点击处理程序与事件委托和克隆一起使用,如下所示

jQuery(function($){
    var _parent = $('.parent').eq(0).clone();
    $(document).on('click', '.inc-child', function(){
        var $p = $(this).parent();    
        $p.find('.child').first().clone().insertBefore(this)
    })
    $('.inc-parent').on('click', function(){
        _parent.clone().insertBefore(this)
    })
})

Demo: Fiddle 演示: 小提琴

Check this JS 检查这个JS

$(document).ready(function() {

$("button[name='addDom']").click(function() {
    var domElement = $('<div class="module_holder"><div class="module_item"><img src="images/i-5.png" alt="Sweep Stakes" /></div></div>');
    $(this).after(domElement);
});

});

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

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