访问jquery克隆元素
我在做这样的事情访问jquery克隆元素,jquery,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 widt
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 ............
});
}
我怀疑问题在于克隆尚未添加到文档中。尝试将其插入DOM,然后获取宽度。您有一个输入错误(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();
}
是的,我也想到了,但后来尝试了$(this.css('width');我认为道格是对的。只有将对象放入文档中,才能知道对象的尺寸。为什么?因为其他元素可能会影响样式。使元素不可见,将其添加到文档中,然后检查宽度。