6

这不起作用:

$block.insertAfter(form);

$('.date', $block).datetime({
     userLang: 'en',
    americanMode: true
});

但这确实:

$block.insertAfter(form);

window.setTimeout(function() {
    $('.date', $block).datetime({
        userLang: 'en',
        americanMode: true
    });
}, 1000);

datetime()是一个插件,我用 class 附加到输入元素date。显然 #1 不起作用,因为该元素在 DOM 中仍然不可用,等待 1 秒有效。但它很老套。如何扩展 jQuery 以接受insertAfter()方法或其他方式的回调?

4

1 回答 1

2

Wrap your code in the document-ready incantation:

$(function() {
    $block.insertAfter(form);

    $('.date', $block).datetime({
         userLang: 'en',
        americanMode: true
    });
});

And make sure this occurs after the datetime library is loaded. That is, this <script> tag should be after the <script src="datetime.js">. And make sure the async attribute is not set on the <script> tags.

于 2013-04-04T17:14:18.063 回答