footLinks is an array of selected DOM elements via jquery. Here's the code im working on:
var footLinks = $('.links li');
for (var i = 0; i < footLinks.length; i++) {
var footLink = footLinks[i];
var footLinkWidth = footLink.width();
console.log('Width: ' + footLinkWidth);
}
How do I get each element's width?
I think it would be better if you wrote it like this:
$('.links li').each(function(d) {
console.log("Width: "+$(this).width())
});
jQuery returns object in array, and when you want to use each DOM
element in loop that is work with JavaScript function, but width()
is not native function, if you want to get width
using jQuery width()
method then create jquery object
like $(footLink)
so you can also use offsetWidth
native method
$(footLink).width();
OR
footLink.offsetWidth;
Use jQuery wrapper for the 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.