如何使用jQuery为页面上的每个表的第一行设置样式 [英] How do I style the first row of every table on the page using jQuery
问题描述
我需要手动设置页面上每个表的第一行的样式。不幸的是我正在处理一些旧的浏览器,所以想通过jQuery这样做。
I need to manually style the first row of every table on a page. Unfortunately I'm dealing with some old browsers so would like to do this via jQuery.
目前我正在使用:
$(function() {
$(".OpenCourse tr:first").addClass("OpenCoursesHeader");
});
然后添加 .OpenCoursesHeader td {}
to样式。
类 OpenCoursesHeader
似乎只适用于第一个表。
the class OpenCoursesHeader
only seems to be applied to the first table.
我是如何将它应用于第二个的?如果这样做更简单,页面上总会有两个表。
Any ideas how I get it to apply to the second? There will always only be two tables on the page if that makes it simpler.
推荐答案
你可以使用第一个孩子
选择器。
首先
只会选择你正在使用的选择器的第一个元素。
You could use the first-child
selector.
first
will only select the first element of the selector you're working with.
$(function() {
$(".OpenCourse tr:first-child").addClass("OpenCoursesHeader");
});
但是如果你有嵌套表,这个解决方案也会将类添加到第一个 tr
。
But if you have nested tables, this solution will also add the class to the first tr
.
这篇关于如何使用jQuery为页面上的每个表的第一行设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!