IE8中的jQuery CSS错误

Elz*_*ugi 9 css jquery internet-explorer-8

我试图从css获取带有背景图像的元素的属性时,IE8中有一个错误.

el.css({ 'background-position': "10px 10px"}); //set some 
alert(el.css("background-position")); //get the value
Run Code Online (Sandbox Code Playgroud)

在Opera,FF,Chrome,Safari的作品中,我获得了"10px 10px".不是在我不明确的IE8中.我打开了一个错误报告,但在那之前你认为这将成为一个很好的解决方案.我该如何以其他方式获得这些价值?

Dan*_*n F 8

应该有所帮助.

它链接到jquery票2462,这也很有趣

编辑第一个链接死,返回机器救援.

而且,以防archive.org曾经打包过,这里是dextrose博客文章的内容.

在询问对象的背景位置时,jQuery(至少直到版本1.2.6)在Internet Explorer中存在问题.

$('h1:first').css('background-position');
Run Code Online (Sandbox Code Playgroud)

在其他A级浏览器中,您将获得2个值(以px或%表示),分别表示元素的x位置和y位置.在Internet Explorer(6和7)中,您未定义.问题是IE不知道背景位置是什么,它只知道其他2个调用:background-position-x和background-position-y.这是一段用于处理此问题的JavaScript代码.

(function($) {
  jQuery.fn.backgroundPosition = function() {
    var p = $(this).css('background-position');
    if(typeof(p) === 'undefined') 
        return $(this).css('background-position-x') 
               + ' ' + $(this).css('background-position-y');
    else return p;
  };
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

您现在可以使用此jQuery插件来获取元素的背景位置:

$('h1:first').backgroundPosition();
Run Code Online (Sandbox Code Playgroud)

  • 第一个链接已经死了. (2认同)