2010-01-07 79 views
0

IAM做這樣的事情訪問jQuery的克隆元素

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 ............ 
     }); 
    } 
+0

它的拼寫是「定製」 – nickf 2010-01-07 14:01:57

回答

2

我懷疑的問題是,克隆尚未被添加到文檔呢。嘗試將其插入到DOM中,然後獲取寬度。

+0

是的,我也想到了,但後來想這 $(本)的.css(」寬度'); – 2010-01-07 13:34:45

+0

我認爲道格是對的。在將對象放入文檔之前,您無法分辨對象的尺寸。爲什麼?因爲其他元素可能會影響風格。使元素不可見,將其添加到文檔中,然後檢查寬度。 – nickf 2010-01-07 14:03:47

0

你有一個錯字(custumResize - > customResize),但你可能已經知道了。

總之,嘗試定義ready事件外插件這樣的:

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

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