JQuery help with if statement

I am trying to scale images with a width greater than 100. I use the code below, but it scales images that are even below 100 pixels ... What am I doing wrong?

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
                var width = $("span span img").width();

                if(width > 100) {
                    $("span span img").cjObjectScaler({
                            destObj: $(".image-attach-body"),
                                method: "fit",
                });
                }
    }); 
    }
}
+3
source share
7 answers

The main problem: you are not viewing the search, so your final one $('span span img')finds everything imgon the page. Here is a feature that fixes other issues as well. Please ask, it makes no sense:

if( $(".image-attach-body").length ) {
    $(".image-attach-body a").each(function() {
        var $img = $("span span img", this),
            width = $img.width();

        if(width > 100) {
            $img.cjObjectScaler({
                destObj: $img.closest(".image-attach-body"),
                method: "fit",
            });
        }
    }); 
}

. if true, jQuery, , null, . , .length , 1. if ( ) , each 0, , ... , , .

+2

100, . ...

$("span span img").cjObjectScaler

. , 100, . , ( jQuery).

, , , . , , this - , .

, ...

$("span span img", this).cjObjectScaler

: , , , . , , " "

+3

$(document).load( );, , .

$(document).load( function () {
 if($(".image-attach-body")) {
        $(".image-attach-body a").each(function() {
         var img = $("span span img",this);

         if(img.width() > 100) {
           img.cjObjectScaler({
             destObj: $(".image-attach-body"),
             method: "fit"
           });
         }
       });

 }

});

0

, width() . :

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
    var width = $("span span img",$(this)).width();
0

, , width, .

, - :

$(".image-attach-body a").each(function() {
    var images = $("span span img").filter(function() {
        return this.width > 100;
    });

    images.cjObjectScaler({
        destObj: $(".image-attach-body"),
        method: "fit"
    });
});
0

, , cjObjectScaler, ( ...)

if($(".image-attach-body")) {
    if($(".image-attach-body a")) {
    $(".image-attach-body a").each(function() {
                var $img = $("span span img", this);
                var width = $img.width();

                if(width > 100) {
                    img.attr("width", "100"); //Obviously, you'll replace it with your cjObjectScaler function here....
                });
                }
    }); 
    }
}
0
source

$("span span img") selects images on the entire page, not in relation to the current area.

Additions, you do not need to check jQuery before use each. This method just does nothing on empty jQuery, so your code can be simplified to:

// For each attach body in document
$(".image-attach-body").each(function(attachBody)
{
    // For each image in the attach body
    $("a span span img", attachBody).each(function() 
    {
        // If image is too large
        if( $(this).width() > 100 )
        {
            // Scale to attach body
            $(this).cjObjectScaler({
                destObj: $(attachBody),
                method: "fit",
            });
        }
    });
});
0
source

Source: https://habr.com/ru/post/1731935/