有关于在jQuery中向动态元素添加动画的问题

时间:2015-07-15 08:05:33

标签: jquery jquery-animate

请您查看this demo并告诉我为什么我无法将.slideDown()函数添加到所选元素的父元素后输入动态div的过程中?



$(function () {
    $("button").on("click", function () {
        $(".err").hide();
        var elem = $(this);
        elem.parent().after('<div class="alert alert-danger err" role="alert">Thanks For Adding</div>').slideDown("slow");
    });
});
&#13;
@import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css");
body {
    padding:50px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="well well-sm box">
    <button class="btn btn-default btn-block" type="submit">Add Box</button>
</div>
<div class="well well-sm box">
    <button class="btn btn-default btn-block" type="submit">Add Box</button>
</div>
<div class="well well-sm box">
    <button class="btn btn-default btn-block" type="submit">Add Box</button>
</div>
<div class="well well-sm box">
    <button class="btn btn-default btn-block" type="submit">Add Box</button>
</div>
<div class="well well-sm box">
    <button class="btn btn-default btn-block" type="submit">Add Box</button>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

这应该有效:)

$(function () {
    $("button").on("click", function () {
        var elem = $(this);
        var $div = $('<div/>',{class: 'alert alert-danger err',role: 'alert'}).hide().text('Thanks For Adding');
        $div.insertAfter(elem.parent());
        $div.slideDown("slow");
    });
});

https://jsfiddle.net/dja0jxm3/1/