【问题标题】:How to get the pixel height of an inline background image via jQuery [duplicate]如何通过 jQuery [重复] 获取内联背景图像的像素高度
【发布时间】:2014-07-12 16:53:15
【问题描述】:

我想通过 jQuery 获取这个 HTML 元素的背景图像的像素高度,如何?

<section class="profile-banner" style="background: url(http://www.mydomain.com/images/myimage.png) 50% 50% no-repeat;"></section>

更新

我现在有了这个,但我从来没有看到高度警报框alert('height:' + height);。 为什么?

    var url = $('.profile-banner').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '');
    alert(url);
    var bgImg = $('<img />');
    bgImg.hide();
    bgImg.bind('load', function()
    {
        var height = $(this).height();
        alert('height:' + height);
    });

【问题讨论】:

    标签: jquery css height inline pixels


    【解决方案1】:

    你应该这样做

    var url = $('#myDiv').css('background-image').replace('url(', '').replace(')', '').replace("'", '').replace('"', '');
    var bgImg = $('<img />');
    bgImg.hide();
    bgImg.bind('load', function()
    {
        var height = $(this).height();
        alert(height);
    });
    $('#myDiv').append(bgImg);
    bgImg.attr('src', url);
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2010-11-25
      • 2020-03-24
      • 2018-12-08
      相关资源
      最近更新 更多