2011-02-13 114 views
0

這是緩存對象以備將來使用的正確方法。Jquery對象緩存

var x = $value.children().andSelf().filter('embed'), 

vsrc = x.attr('src'), 
vwidth = x.attr('width'), 
vheight = x.attr('height'); 

回答

3

是。

但是,你可以提高你的格式:

var x = $value.children().andSelf().filter('embed'), 
    vsrc = x.attr('src'), 
    vwidth = x.width(), 
    vheight = x.height(); 

此外,您還可以使用width()height()檢索元素的尺寸。

2

是的,這正是如何「重用」一個jQuery對象,如果你不想鏈接它。 (如果過度使用,Chaining可能會使代碼難以閱讀,所以這通常是一個很好的選擇。)

雖然根本沒有標準,但爲了給變量起一個名字$以標記爲一個jQuery對象:

var $embed = $value.children().andSelf().filter('embed'); 

(Joel on "apps hungarian" notation.)

+0

很好,逗號用於添加其他變量而不需要再次使用var – Hussein 2011-02-13 03:36:39