footLinks是通过jquery选择的DOM元素的数组。下面是我正在处理的代码:
var footLinks = $('.links li');
for (var i = 0; i < footLinks.length; i++) {
var footLink = footLinks[i];
var footLinkWidth = footLink.width();
console.log('Width: ' + footLinkWidth);
}
如何获得每个元素的宽度?
发布于 2015-01-08 09:28:36
我觉得你最好这样写:
$('.links li').each(function(d) {
console.log("Width: "+$(this).width())
});
发布于 2015-01-08 09:37:26
jQuery返回数组中的对象,当您想在循环中使用与JavaScript函数一起工作的每个DOM
元素,但width()
不是本机函数时,如果希望使用jQuery width()
方法获得width
,则创建jquery object
类$(footLink)
,以便也可以使用offsetWidth
本机方法。
$(footLink).width();
或
footLink.offsetWidth;
发布于 2015-01-08 09:31:06
对jQuery使用footLink
包装器
var footLinks = $('.links li');
for (var i = 0; i < footLinks.length; i++) {
var footLink = footLinks[i];
var footLinkWidth = $(footLink).width();
console.log('Width: ' + footLinkWidth);
}
https://stackoverflow.com/questions/27836700
复制相似问题