0

私は非常にまれな問題を抱えているか、初心者なのでわかりません:)

ajax を使用して DOM ツリーを作成しています。呼び出している関数が機能しないことを除けば、出力は完璧です。. 純粋な JavaScript で同じ 3 つを作成するとします。実際に関数を呼び出します。説明するのはかなり難しいですが、いくつかのコードで示します。

function stickers(){
    $(document).ready(function() {
        $('#add-new-sticker-btn').click(function() {
            $.get('xml/data.xml', function(data) {
                $('#page-content-wrapper').empty();

                $(data).find('car').each(function() {
                    var $car = $(this);
                    var sticker = '<div class="sticker">';
                    sticker += '<div class ="sticker-drag">' + '</div>';
                    sticker += '<textarea>' + $car.find('product').text() + '</textarea>';
                    sticker += '<div class="sticker-close">' + '</div>';

                    $('#page-content-wrapper').append(sticker);
                });
            });
            return false;
        });
    }); 
    movewrap();  // <!-- this is the function that I'm trying to call.
}

しかし、代わりに純粋なjavascriptを書くと

function stickers(){
    var sticker = createElementWithClass('div', 'sticker'),
    textArea = document.createElement('textarea');
    var stickerDrag = createElementWithClass('div','sticker-drag')
    var stickerClose = createElementWithClass('div','sticker-close')

    sticker.appendChild(stickerDrag);
    sticker.appendChild(textArea);
    sticker.appendChild(stickerClose);

    document.getElementById('page-content-wrapper').appendChild(sticker);

    movewrap(); 
} // its calling the moveWrap function.

何か案は ?

4

1 に答える 1

2

moveWrapAJAX コールバックの最後にへの呼び出しを配置し​​ます。現在、応答を受信した後ではなく、要求が行われた後に呼び出されており、DOM 要素がまだ存在しないため、実行することはありません。

于 2013-03-14T16:07:07.173 に答える