Simple JQuery script working on JSFiddle, not on my site

后端 未结 4 641
有刺的猬
有刺的猬 2021-01-20 03:02

I\'m trying to get the original width of an image with JQuery and make some adjustments in the CSS with a condition if an image is wider than 700px.

I used this code

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 03:20

    Try it like this:

    var img = new Image();
    img.onload = function () {
        var W2 = this.width;
        if(W2 > 700) {
            var photoHolder = document.getElementById("photoHolder");
            photoHolder.style.verticalAlign = 'none';
            photoHolder.style.textAlign = 'none';
        }
    }
    img.src = document.getElementById('imageViewerImg').src;
    

    Also, open the console and check for errors, check that you only have one element with that ID etc.

提交回复
热议问题