Jquery 我可以使用yepnope/modernizer.load()检测DOM插入吗

Jquery 我可以使用yepnope/modernizer.load()检测DOM插入吗,jquery,modernizr,yepnope,Jquery,Modernizr,Yepnope,在windows窗体应用程序中,我们有一个Master.js文件,该文件在加载Modernizer后被引用 我在masterpage.js底部的$(document).ready函数中声明了我的modernizer.load测试,它可以根据我需要的测试加载我的js文件 i、 e 但是,我希望这个测试可以在document.ready上完成,也可以在DOM插入点上完成,这之后可能会发生。这在Modernizer或jQuery中可能吗 我的目标是在我的masterpage.js中声明所有modern

在windows窗体应用程序中,我们有一个Master.js文件,该文件在加载Modernizer后被引用

我在masterpage.js底部的$(document).ready函数中声明了我的modernizer.load测试,它可以根据我需要的测试加载我的js文件

i、 e

但是,我希望这个测试可以在document.ready上完成,也可以在DOM插入点上完成,这之后可能会发生。这在Modernizer或jQuery中可能吗


我的目标是在我的masterpage.js中声明所有modernizr测试,而不是在将来可能插入需要在测试中加载资源的DOM元素时重新声明测试。

首先,
modernizr.addTest()
在您的情况下并不是真正必要的(它将锁定并可能导致不必要的行为). 你可以简单地做:

Modernizr.load({
    test: $('section #myid').length > 0,
    ...
});
尽管您可能完全可以跳过它,这取决于您监视DOM的方式有助于:

$('section #myid').livequery(function () {
    // This will run whenever a new element matches the selector: either now
    // (if the element already exists), or when it's inserted into the DOM later
    Modernizr.load({
        // no test needed - just use it as an async loader
        load: ['custom1.js', 'custom2.js'],
        complete: function () {
            // do stuff
        }
    });

    // Presumably you only want the above to run once, so remove the Livequery
    // handler now (`arguments.callee` is the current function, i.e. the event
    // handler)
    $('section #myid').expire(arguments.callee);
});

哦,
$('myid')
会稍微快一点。如果您只想在
#myid
作为
元素的后代加载时执行该操作,那么在livequery函数中测试该条件会更具可读性。cheers Stu,这似乎是迄今为止最好的选择,尽管看起来(查看源代码)基本上每20毫秒运行一次查询。因此,而不是响应DOM插入事件(它似乎不存在),它基本上检查直到选择器是真的。您可以考虑——在一些浏览器中的一个新特性,它可以监视DOM而不连续查询-如果没有可用,则返回到LIVQUEST?以下是如何检测支持:
$('section #myid').livequery(function () {
    // This will run whenever a new element matches the selector: either now
    // (if the element already exists), or when it's inserted into the DOM later
    Modernizr.load({
        // no test needed - just use it as an async loader
        load: ['custom1.js', 'custom2.js'],
        complete: function () {
            // do stuff
        }
    });

    // Presumably you only want the above to run once, so remove the Livequery
    // handler now (`arguments.callee` is the current function, i.e. the event
    // handler)
    $('section #myid').expire(arguments.callee);
});