开发者

accessing jquery cloned element

开发者 https://www.devze.com 2022-12-15 13:59 出处:网络
iam doing something like this var _orgElm = $(this).clone(true); _orgElm.customResize(); $.fn.custumResize = function() {

iam doing something like this

    var _orgElm = $(this).clone(true);
    _orgElm.customResize();

    $.fn.custumResize = function() {
        return this.each(function() {
            // problem is here
            // when i apply this function an element, it reads width correctly. 
            //but when i do this on elements clone it reads zero is there is,
            // anyway i can read clone width also
            $(this).width();
            //some code here ............
        });
开发者_Python百科    }


I suspect the problem is that the clone has not been added to the document yet. Try inserting it in the DOM and then get the width.


You have a typo (custumResize -> customResize), but you probably already know that.

Anyhow, try defining your plugin outside the ready event like this:

$.fn.customResize = function() {
    return this.each(function() {
        alert($(this).width());
    });
}

$(function() {
    var elem = $('.some-element');
    var clone = elem.clone();
    elem.customResize();
    clone.customResize();
}
0

精彩评论

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