开发者

jQuery wrap is forcing multiple document ready callbacks

开发者 https://www.devze.com 2023-01-02 10:30 出处:网络
I have the following code: function CreateRoundbox(selector, scope) { $(selector).wrap(\'<div class=\\\"\' + scope + \' dialog\\\"><div class=\\\"bd\\\"><div class=\\\"c\\\"><div

I have the following code:

function CreateRoundbox(selector, scope) {
    $(selector).wrap('<div class=\"' + scope + ' dialog\"><div class=\"bd\"><div class=\"c\"><div class=\"s\"></div></div></div></div>');
    $('div.' + scope).prepend('<div class=\"hd\"><div class=\"c\"></div></div>').append('<div class=\"ft\"><div class=\"c\"></div></div>');
}

Whenever I use this function and the "wrap" method is called it causes the $开发者_如何学运维(document).ready() method to fire for a second time on the page. Here is the call to the function (this lives in a document ready block):

CreateRoundbox(".roundbox", "roundbox-wrapper");      

Has anyone ran into this before? Am I using wrap wrong?

UPDATE

Fixed usage line, I accidentally added a dot in the scope literal


function CreateRoundbox(selector, scope) {
                            //           v----> this will result to class=".roundbox-wrapper", which is, I guess, not the thing you want in there.. :)
    $(selector).wrap('<div class=\"' + scope + ' dialog\"><div class=\"bd\"><div class=\"c\"><div class=\"s\"></div></div></div></div>');
    $('div.' + scope).prepend('<div class=\"hd\"><div class=\"c\"></div></div>').append('<div class=\"ft\"><div class=\"c\"></div></div>');
   //     ^    ^
   //     |    |--- look at scope.... then, look here at the second argument, CreateRoundbox(".roundbox", ".roundbox-wrapper");
   //     |--- look at the dot.... :)   CreateRoundbox(".roundbox", ".roundbox-wrapper");

}
0

精彩评论

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

关注公众号