jQuery在IE8中的CSS错误 [英] jQuery CSS bug in IE8

查看:73
本文介绍了jQuery在IE8中的CSS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IE8中有一个bug,当尝试从css获取一个背景图片元素的属性。

I have a bug in IE8 when trying to get a property from css for an element with background image.

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

在Opera,FF,Chrome,Safari工作中,我得到10px 10px。不是在IE8,我得到未定义。
我打开了一个错误报告,但直到那时,你认为它会是什么一个好的解决这个问题。我应该如何以另一种方式获得这个值?

In Opera, FF, Chrome, Safari works I get "10px 10px". Not in IE8 where I get undefined. I opened a bug report, but until then what do you think it will be a good workaround this problem. How should I get this values in some another way?

推荐答案

this should help。

this should help.

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

编辑第一个链接已失效,回送机来救援。

Edit first link dead, wayback machine to the rescue.

而且,只是在archive.org过来的情况下,这里是dextrose博客文章的内容。

And, just in case archive.org ever packs up, here is the contents of the dextrose blog post.

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

jQuery (at least up till version 1.2.6) has a problem with Internet Explorer when asking the background-position of an object.

$('h1:first').css('background-position');

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

In the other A-grade browsers, you get 2 values (in px or %) representing respectively the x-position and y-position of the element. In Internet Explorer (6 and 7) you get undefined. The problem is that IE doesn't know what background-position is, it only knows 2 other calls: background-position-x and background-position-y. Here is a slice of JavaScript code to handle this problem.

(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);

现在可以使用这个jQuery插件获取
的元素的背景位置:

You can now use this jQuery plugin to get the background-position of an element:

$('h1:first').backgroundPosition();


这篇关于jQuery在IE8中的CSS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆