jQuery addClass 似乎无法在 Internet Explorer 中工作

发布于 2024-11-01 03:27:52 字数 968 浏览 2 评论 0原文

我整个下午都在试图解决这个问题,但没有成功。在我正在工作的这个网站上(http://chezkoop.ca/united),我们有几个领域(主页第一列和第二列以及事件页面)利用 css 伪选择器 :nth-child() 为各个行着色。

显然,nth-child() 在 Internet Explorer 8 及以下版本中不起作用(还没有看过 IE9...),所以我想使用以下代码通过 jQuery 复制此功能(这是放在 $(document).ready(function(){ ... });) 中:

$(".post:nth-child(even)").addClass("latestpost-even");
$(".dbem_events_list li:nth-child(2n-1)").addClass("events-odd-row");
$("tr:nth-child(2n+1)").addClass("calendar-odd-row");
$("tr:nth-child(1)").addClass("calendar-first-row");

然后我在 CSS 中定义了这些类,如下所示(这是仅第一个示例):

.post:nth-child(even), .latestpost-even {
background-color: #f5f4e8;
}

如果我使用 Firebug 检查 Firefox 中的 DOM,这些类已正确应用(尽管没有必要,因为我使用的是 Firefox)。在 Internet Explorer 8 或 7 中查看页面时,行没有着色(因此可能未应用这些类)。

整个下午都在试图解决这个问题,但没有成功。我在互联网上进行了搜索,但没有得到任何结果。如果有人对此有任何见解,那就太棒了。

谢谢

阿德里安

I have been trying to figure this out all afternoon with no luck. On this site I am working on (http://chezkoop.ca/united) we have a couple of areas (homepage columns one and two and the events page) which utilize the css pseudo selector :nth-child() to colour various rows.

Obviously, nth-child() does not work in Internet Explorer 8 and down (haven't looked at IE9 yet...) so I want to replicate this functionality with jQuery using the following (this being place inside $(document).ready(function(){ ... });):

$(".post:nth-child(even)").addClass("latestpost-even");
$(".dbem_events_list li:nth-child(2n-1)").addClass("events-odd-row");
$("tr:nth-child(2n+1)").addClass("calendar-odd-row");
$("tr:nth-child(1)").addClass("calendar-first-row");

I have then defined those classes in my CSS like this (this is the first example only):

.post:nth-child(even), .latestpost-even {
background-color: #f5f4e8;
}

If I check the DOM in Firefox with Firebug these classes have been applied correctly (although unnecessarily, because I'm in Firefox). When viewing the page in Internet Explorer 8 or 7, the rows are not coloured (so presumably the classes are not being applied).

Been trying to figure this out all afternoon with no luck. I've had a search through the interwebs and haven't come up with anything. If anyone has any insight into this that would be fantastic.

Thanks

Adrian

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

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

发布评论

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

评论(4

骄傲 2024-11-08 03:27:52

我可以在 IE 的开发人员工具中看到该类已添加,无论是在 IE7 还是 IE8 兼容模式下。

也许 IE 忽略了它不理解的行,所以你可以尝试:

.post:nth-child(even) {
  background-color: #f5f4e8;
}
.latestpost-even {
  background-color: #f5f4e8;
}

或者甚至更好:

.latestpost-even, .post:nth-child(even) {
  background-color: #f5f4e8;
}

编辑:顺便说一下,我正在查看 .events-odd-row 而不是 .latestpost-even 但同样的原则适用。

I can see in the Developer Tools in IE that the class gets added, both in IE7 and IE8 compatibility mode.

Perhaps IE is ignoring the line it doesn´t understand, so you could try:

.post:nth-child(even) {
  background-color: #f5f4e8;
}
.latestpost-even {
  background-color: #f5f4e8;
}

or, even better:

.latestpost-even, .post:nth-child(even) {
  background-color: #f5f4e8;
}

Edit: By the way, I was looking at .events-odd-row and not .latestpost-even but the same principle applies.

烈酒灼喉 2024-11-08 03:27:52

而不是:

.post:nth-child(even), .latestpost-even {
   background-color: #f5f4e8;
}

尝试

.post:nth-child(even) {background-color: #f5f4e8;}

.latestpost-even {background-color: #f5f4e8;}

IE对于那些它不理解的伪类也有一点小缺陷,因为如果它有一个它不理解的选择器,它会忽略整个规则集

instead of :

.post:nth-child(even), .latestpost-even {
   background-color: #f5f4e8;
}

try

.post:nth-child(even) {background-color: #f5f4e8;}

.latestpost-even {background-color: #f5f4e8;}

IE also has a little foible with those pseudos that it doesn't understand in that it will ignore the whole ruleset if it has a selector it doesn't understand

伊面 2024-11-08 03:27:52

您是否尝试过

$(".post:even")

$(".post").even()

(后者需要扩展代码 - 请参阅 api 注释...)

http://api .jquery.com/even-selector/

Have you tried

$(".post:even")

or

$(".post").even()

(latter would require extended code -- see api comments...)

http://api.jquery.com/even-selector/

葬シ愛 2024-11-08 03:27:52

使用 Internet Explorer 7 时,我在使用 jQuery :odd & 条带化表格时遇到了麻烦。 :甚至添加类。另外,需要更改所选行的背景颜色。

它会将类添加到行中(使用动态 DOM 检查器 PageSpy 检查),但看不到效果。

我需要向行中的每个单元格添加相同的类才能使其正常工作。

发现了 jQuery 的 andSelf。

CSS

.odd{background-color:#fafafa;}

.even{background-color:#f4f4f4;}

.selected_row{background-color:#ff9999;}

Javascript

$('tbody#interest_list_body tr:odd').find('td').andSelf().addClass('odd');      
$('tbody#interest_list_body tr:even').find('td').andSelf().addClass('even');

和对于选定的行,我使用了切换方法:

$('tbody#interest_list_body tr').toggle(  
   function(){  
    $('tbody#interest_list_body tr').find('td').andSelf().removeClass('selected_row');  
    if($(this).attr('id')){  
          $(this).find('td').andSelf().addClass('selected_row');  
    } // end if  
    }, // end fn  
   function(){  
          $(this).find('td').andSelf().removeClass('selected_row');  
    } // end fn  
);  

希望这对某人有帮助

Using Internet Explorer 7, I had the trouble of striping a table using jQuery :odd & :even addClass. Also, needed to change the background color of a selected row.

It would add the class to the row (checked using PageSpy, an inspector of dynamic DOM) but the effect would not be seen.

I needed to add the same class to each cell in the row for it to work.

Discovered jQuery's andSelf.

CSS

.odd{background-color:#fafafa;}

.even{background-color:#f4f4f4;}

.selected_row{background-color:#ff9999;}

Javascript

$('tbody#interest_list_body tr:odd').find('td').andSelf().addClass('odd');      
$('tbody#interest_list_body tr:even').find('td').andSelf().addClass('even');

and for the selected rows I used a toggle approach:

$('tbody#interest_list_body tr').toggle(  
   function(){  
    $('tbody#interest_list_body tr').find('td').andSelf().removeClass('selected_row');  
    if($(this).attr('id')){  
          $(this).find('td').andSelf().addClass('selected_row');  
    } // end if  
    }, // end fn  
   function(){  
          $(this).find('td').andSelf().removeClass('selected_row');  
    } // end fn  
);  

Hope this helps someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文