0

我正在尝试从 jquery plugins basic book 学习创建插件,但是以下插件不显示图像的高度它总是显示 0 图像高度我应该怎么做才能获得图像高度

(function($) {
    jQuery.fn.gallery = function() {
        return this.each(function() {
            var e = $(this);
            console.log(e);
            var h = e.height();
            console.log(h);
            var w = e.width();
            //     e.height(40);
            //       e.width(40);
            e.click(function() {
                console.log(h);
                //    $('#gal').remove();
                e.clone().prependTo("body").center().click(function() {
                    $(this).remove();
                });

            });
        });
    };
})(jQuery);​
4

1 回答 1

0

试试这个

(function($) {
    jQuery.fn.gallery = function() {
        return this.each(function() {
            var e = $(this);
            console.log(e);
            var h = e.attr("height");
            console.log(h);
            var w = e.attr("width");              
            e.click(function() {
                console.log(h);
                //    $('#gal').remove();
                e.clone().prependTo("body").center().click(function() {
                    $(this).remove();
                });

            });
        });
    };
})(jQuery);​

在 <img> 标签中使用 width 和 height 属性, 或者

clientWidth 和 clientHeight 是 DOM 属性,显示 DOM 元素内部尺寸的当前浏览器内大小(不包括边距和边框)。因此,对于 IMG 元素,这将获得可见图像的实际尺寸。

var img = document.getElementById('imageid'); 
//or however you get a handle to the IMG
var width = img.clientWidth;
var height = img.clientHeight;
于 2012-04-30T07:07:03.360 回答