jQuery addClass 似乎无法在 Internet Explorer 中工作
我整个下午都在试图解决这个问题,但没有成功。在我正在工作的这个网站上(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以在 IE 的开发人员工具中看到该类已添加,无论是在 IE7 还是 IE8 兼容模式下。
也许 IE 忽略了它不理解的行,所以你可以尝试:
或者甚至更好:
编辑:顺便说一下,我正在查看
.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:
or, even better:
Edit: By the way, I was looking at
.events-odd-row
and not.latestpost-even
but the same principle applies.而不是:
尝试
IE对于那些它不理解的伪类也有一点小缺陷,因为如果它有一个它不理解的选择器,它会忽略整个规则集
instead of :
try
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
您是否尝试过
或
(后者需要扩展代码 - 请参阅 api 注释...)
http://api .jquery.com/even-selector/
Have you tried
or
(latter would require extended code -- see api comments...)
http://api.jquery.com/even-selector/
使用 Internet Explorer 7 时,我在使用 jQuery :odd & 条带化表格时遇到了麻烦。 :甚至添加类。另外,需要更改所选行的背景颜色。
它会将类添加到行中(使用动态 DOM 检查器 PageSpy 检查),但看不到效果。
我需要向行中的每个单元格添加相同的类才能使其正常工作。
发现了 jQuery 的 andSelf。
CSS
Javascript
和对于选定的行,我使用了切换方法:
希望这对某人有帮助
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
Javascript
and for the selected rows I used a toggle approach:
Hope this helps someone