简体   繁体   English

jQuery不会更改Internet Explorer中的类或CSS

[英]jQuery not changing class or CSS in Internet Explorer

I have an image slideshow, which has a small DIV on top of it for ad copy. 我有一个图片幻灯片,其顶部有一个小DIV,可用于广告文案。 Every time the slide changes, the ad box fades out, and a new one fades in, in a different position. 每次幻灯片更改时,广告框都会淡出,而新的框会淡入另一个位置。

This works great in every browser but IE. 这在除IE之外的所有浏览器中都很好用。 All of my jQuery animations are working fine, but for some reason the box won't change position. 我所有的jQuery动画都能正常工作,但是由于某些原因,该框不会改变位置。 Both .css() to change my top/left values and actually changing the class using .attr() fail in IE. 这两个.css()都可以更改我的顶部/左侧值,并且实际上使用.attr()来更改类均在IE中失败。

Here is the JS to change the class, and show the box. 这是更改类并显示框的JS。 The first line simply gets the position from the filename (image-6.jpg). 第一行只是从文件名(image-6.jpg)中获取位置。

var heroPosition = vars.currentImage.attr('src').substr(-5, 1);
switch(heroPosition) {
  case "1":
    $('#hero-ad').attr('class','hero-position-1');
    break;
  case "2":
    $('#hero-ad').attr('class','hero-position-2');              
    break;
  case "3":
    $('#hero-ad').attr('class','hero-position-3');
    break;
  case "4":
    $('#hero-ad').attr('class','hero-position-4');
    break;
  case "5":
    $('#hero-ad').attr('class','hero-position-5');
    break;
  case "6":
    $('#hero-ad').attr('class','hero-position-6');
break;
}
$('#hero-ad').show("fold", {}, 1000);

The code itself was sort of hacked into the Nivo image slider. 该代码本身被黑客入侵了Nivo图像滑块。

The live site is here http://www.sugarloaf.com/slredesignlocal/index.html 实时站点位于http://www.sugarloaf.com/slredesignlocal/index.html

Most of the JS in js/plugins.js. js / plugins.js中的大多数JS。 CSS for the positions is in css/style.css 位置的CSS在css / style.css中

EDIT I should mention that I tried using .addClass() and .removeClass() and this method also failed. 编辑我应该提到我尝试使用.addClass()和.removeClass(),并且此方法也失败了。

I'm guessing you are having this problem specifically in IE because you are using substr() with a negative number. 我猜想您是在IE中专门遇到此问题,因为您使用的是带有负数的substr() Check out this post . 看看这个帖子 You should consider using slice() instead to get the value you're expecting. 您应该考虑使用slice()来获取期望的值。

Use .addClass( className ) to change css classes. 使用.addClass( className )更改CSS类。

eg 例如

$('#hero-ad').addClass('hero-position-1')

Depending on version of IE, class may not be a valid attribute. 根据IE的版本, class可能不是有效的属性。

如果要替换类而不只是添加新类,请尝试此操作。

$('#hero-ad')[0].className = 'hero-position-1';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM