如何使用 IE6 检索元素的当前背景图像值?

发布于 2024-10-31 22:08:37 字数 509 浏览 3 评论 0原文

这有点令人抓狂。我有代码将所选元素中的当前背景图像保存到变量中,然后用它来创建 img 标签。

简而言之,以下内容适用于我测试过的所有浏览器(IE6 除外):

var bg = $('.element_selector').css('background-image');

IE6 的返回值是“none”,这是不正确的。 (在有人建议尝试“backgroundImage”而不是“background-image”之前,没有骰子。)

关于如何获得该值有什么想法吗?

更新:我忘了提及有问题的背景图像是由 DD_belatedPNG< 处理的/a>,现在看来是罪魁祸首——如果我注释掉修复程序,我就会得到我的值。如果有人知道在 png 修复后如何仍然可以获取该值,请告诉我。

This is a bit maddening. I have code which saves the current background-image from a selected element into a variable, which I then use to create an img tag.

Simply put, the following works in all browsers I've tested except IE6:

var bg = $('.element_selector').css('background-image');

The return value from IE6 is 'none', which is incorrect. (Before anyone suggests trying 'backgroundImage' instead of 'background-image', no dice.)

Any ideas on how I can get that value?

Update: I forgot to mention that the background image in question was processed by DD_belatedPNG, which now appears to be the culprit -- if I comment out the fix, I get my value. If anyone knows offhand how I could still fetch that value after the png fix is in, please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2024-11-07 22:08:38
  1. 您尝试过背景吗?
  2. 您确定元素正确吗?
  1. Have you tried background?
  2. Are you sure the element is right?
柏拉图鍀咏恒 2024-11-07 22:08:37

在 IE6 中进行一些测试后,我能够使用 DD_belatedPNG 和背景 src 重现您的问题。

这是我发现的。如果您在调用 DD_belatedPNG.fix('[selector]'); 之前设置了 background-image 变量,那么您将获得所需的结果。如果您稍后在 click 事件中设置它,您会得到一个值 “none”

此外,DD_belatedPNG 会向包含背景图像的页面添加一个图像元素(具有 DD_belatedPNG_sizeFinder 类),因此您可以 1) 重复使用、2) 复制或 3) 获取 该元素的 src 属性值。

这是我的测试代码:

CSS:

#myElement
{
    width: 515px;
    height: 341px;
    margin: 100px auto;
    background: #f00 url('http://www.chismbrothers.com/images/woodfloors.png') no-repeat 0 0;
}

HTML:

<div id="myElement"></div>


<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<div id="test4"></div>

jQuery:

$(document).ready(function() {
    // Works as expected
    var bg = $('#myElement').css('background-image');
    $('#test1').text("pre png fix: " + bg);

    DD_belatedPNG.fix('#myElement');

    $('#myElement').click(function() {
        var bg = $(this).css('background-image');
        var bgAll = $(this).css('background');

        // This yields values of 'none' for bg and 'undefined' for bgAll
        $('#test2').html("post png fix: " + bg + "<br />bg: " + bgAll);

        // DD_belatedPNG creates an image element on the page with a class of 
        // 'DD_belatedPNG_sizeFinder' which can be used to get the image src
        $('#test3').html('dd_belatedpng_sizeFinder: ' + $('.DD_belatedPNG_sizeFinder').attr("src"));

        // Perhaps reapplying 'zoom' css will reapply the BG values?
        $(this).css({ zoom: 0 }).css({ zoom: 1 });
        // Set values again.
        bg = $(this).css('background-image');
        bgAll = $(this).css('background');
        // No luck...same result as #test2
         $('#test4').html("post png fix: " + bg + "<br />bg: " + bgAll);
    });
});

After some testing in IE6 I was able to reproduce your issue with DD_belatedPNG and background src.

Here's what I've found. If you set the background-image variable before calling DD_belatedPNG.fix('[selector]'); then you get the desired result. If you're setting it afterward for instance in a click event you get a value of "none".

Additionally, DD_belatedPNG adds an image element to the page containing your background image (with a class of DD_belatedPNG_sizeFinder), so you can potentially 1) re-use, 2) copy, or 3) take src attribute value from that element.

Here's my testing code:

CSS:

#myElement
{
    width: 515px;
    height: 341px;
    margin: 100px auto;
    background: #f00 url('http://www.chismbrothers.com/images/woodfloors.png') no-repeat 0 0;
}

HTML:

<div id="myElement"></div>


<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<div id="test4"></div>

jQuery:

$(document).ready(function() {
    // Works as expected
    var bg = $('#myElement').css('background-image');
    $('#test1').text("pre png fix: " + bg);

    DD_belatedPNG.fix('#myElement');

    $('#myElement').click(function() {
        var bg = $(this).css('background-image');
        var bgAll = $(this).css('background');

        // This yields values of 'none' for bg and 'undefined' for bgAll
        $('#test2').html("post png fix: " + bg + "<br />bg: " + bgAll);

        // DD_belatedPNG creates an image element on the page with a class of 
        // 'DD_belatedPNG_sizeFinder' which can be used to get the image src
        $('#test3').html('dd_belatedpng_sizeFinder: ' + $('.DD_belatedPNG_sizeFinder').attr("src"));

        // Perhaps reapplying 'zoom' css will reapply the BG values?
        $(this).css({ zoom: 0 }).css({ zoom: 1 });
        // Set values again.
        bg = $(this).css('background-image');
        bgAll = $(this).css('background');
        // No luck...same result as #test2
         $('#test4').html("post png fix: " + bg + "<br />bg: " + bgAll);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文