开发者

Passing Parameters to jquery ready handler

I would like to create a single function to automate htmlwireups. Some portions of the page are loaded using ajax and I would like to call a single function to ready both the document and the lazily loaded sections.

Example:

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").acc开发者_开发问答ordion();

    }
}


Simply pass the target variable through to the inner function without referencing it in the jQuery ready call.

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();

    });
}

The target variable will be available within the function that's attached to the ready handler due to a closure.

Side note, $(document).ready(yourFunction) or $(yourFunction) are preferred to $().ready(yourFunction), though they are all equivalent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜