将参数传递给jquery ready handler

时间:2010-12-10 21:20:53

标签: jquery ajax

我想创建一个单独的函数来自动化htmlwireups。页面的某些部分是使用ajax加载的,我想调用一个函数来准备文档和延迟加载的部分。

示例:

function WireHtml(target){
    $(target).ready(function(target){

        // call anything you would normally wire in a standard ready
        // but only on the descendants of the target node such as wiring up
        // an accordion
        $(target).find(".accordion").accordion();

    }
}

1 个答案:

答案 0 :(得分:2)

只需将target变量传递给内部函数,而无需在jQuery ready调用中引用它。

function WireHtml(target){
    $(function(){ // <- note the lack of "target" references

        // call anything you would normally wire in a standard ready
        // but only on the descendants of the target node such as wiring up
        // an accordion
        $(target).find(".accordion").accordion();

    });
}

由于closuretarget变量将在附加到就绪处理程序的函数中可用。

旁注,$(document).ready(yourFunction)$(yourFunction)优先于$().ready(yourFunction),但它们都相同。

相关问题