我想创建一个单独的函数来自动化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();
}
}
答案 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();
});
}
由于closure,target
变量将在附加到就绪处理程序的函数中可用。
旁注,$(document).ready(yourFunction)
或$(yourFunction)
优先于$().ready(yourFunction)
,但它们都相同。