为什么InsertAfter在这种情况下不起作用

时间:2015-12-24 17:30:03

标签: jquery

我正在展示提示div,而且我想要显示'更新'提示div末尾的消息。我这样试试:

var sampledata = [{
    "name": "John",
    "tip": "BUY 111",
    "time": "23-DEC"
},{
    "name": "John",
    "tip": "BUY 232",
    "time": "23-DEC"
}]

$(function() {
    $(".cl").html(""); // Clearing the HTML
    for (var i = 0; i < sampledata.length; i++) {
        var name = sampledata[i].name;
        var tip = sampledata[i].tip;
        var time = sampledata[i].time;
        var html = '';

        html += '<a href="#" class="c1">\
                <div class="stoc5 mt1">\
                    <div class="c1 fl wi80">\
                        <span class="fl c2 f14 ti-s mt">' + time + '</span>\
                        ' + tip + '\
                    </div>\
                    <div class="fr c2 f14 ti-s1 mt"></div>\
                    <div class="cl"></div>\
                </div>\
            </a>';
        $(".cl").append(html);
    }

    var newhtml =  '<div class="updated_attipes">23-DEC-2015</div>';
    $(".cl").insertAfter(newhtml);
});

请你告诉我如何在提示div后显示更新的div? https://jsfiddle.net/pL7xugg1/28/

2 个答案:

答案 0 :(得分:0)

insertAfter()方法用于将所选元素放在另一个元素之后,这意味着您试图将<{1}}元素放在内存中的字符串之后 - 因此它不起作用。

您需要仅使用.cl方法将生成的HTML字符串放在after()元素之后。试试这个:

.cl

或者您可以继续使用$(".cl").after(newhtml); 方法,但交换选择器和变量的顺序:

insertAfter

Updated fiddle

答案 1 :(得分:0)

试试这个,这是jQuery insertAfter()

的正确格式
$(newhtml).insertAfter(".cl");

参考insertAfter http://api.jquery.com/insertafter/

的jQuery文档

html中有两个带".cl"类的元素。检查是否可取。更新的小提琴在这里https://jsfiddle.net/pL7xugg1/33/