How can I add class "equal" if an image that is equal in width and height.
Here is my code:
var img = $('img', this);
var width = img.width();
var height = img.height();
if(width == height){
$(this).img.addClass('equal');
}
This bit:
if(width == height){
$(this).img.addClass('equal');
}
should be:
if(width == height){
img.addClass('equal');
}
as you already have your img
as a jQuery object (by doing var img = $('img', this);
)
Iterate and add the class.
//get all images let $images = $('img'); // add classes $.each($images, function(key, img) { if ( img.height === img.width ) { $(img).addClass('equal'); } });
Example: https://jsfiddle.net/6ot4ks87/
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.