关于CSS选择器的问题

发布于 2024-11-30 16:37:46 字数 246 浏览 0 评论 0原文

如果我有一个选择器,例如

#myTable tr td .someClass{....}

此 someClass 是否“仅”引用具有该类的 td 或者它是否引用具有“someClass”的 td 的任何子级?那么它与 ; 一样吗?

#myTable tr td.someClass{....}

基本上我的问题是 td 后面的空格有什么区别吗?

If I have a selector like

#myTable tr td .someClass{....}

Does this someClass refer "ONLY" to td having that class OR does it refer to any child of td having "someClass" ? So Is it same as ;

#myTable tr td.someClass{....}

Basically my question is does the space after td make any difference?

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

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

发布评论

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

评论(7

眉黛浅 2024-12-07 16:37:46

#myTable tr td .someClass{....} 指的是 任何元素,其内部具有 someClass strong> 一个 td 标记,位于 tr 标记内,该标记位于 id 为 myTable 的元素内。

#myTable tr td.someClass{....} 引用带有someClass类的td标签,位于 tr 标记内,该标记位于 id 为 myTable 的元素内。

#myTable tr td .someClass{....} refers to any element with a class of someClass that is inside a td tag, that is inside a tr tag, that is inside an element with an id of myTable.

#myTable tr td.someClass{....} refers to a td tag with a class of someClass, that is inside a tr tag, that is inside an element with an id of myTable.

余厌 2024-12-07 16:37:46

是的,空间会有所不同:

td.someClass {...} //Refers to a table cell with the class someClass
td .someClass {...} //Refers to an element with the class someClass that is a descendant of a table cell

Yes, the space makes a difference:

td.someClass {...} //Refers to a table cell with the class someClass
td .someClass {...} //Refers to an element with the class someClass that is a descendant of a table cell
姐不稀罕 2024-12-07 16:37:46

前一个选择器引用 someClass 类的元素,它是 td 的后代。

要引用类名 someClasstd 元素,请使用后一个选择器。

空格是一个后代选择器;如果您想要一个直接子元素(td.someClass 元素之间没有元素),请使用 >

#myTable tr td > .someClass{....}

The former selector refers to an element of class someClass that's a descendant of a td.

To refer to a td element of class-name someClass use the latter selector.

The space is a descendent selector; if you want an immediate child (no elements between the td and the .someClass element) use >:

#myTable tr td > .someClass{....}
薄荷梦 2024-12-07 16:37:46

孩子和父母之间有一个空间。如果没有空格,您将告诉 CSS 查找具有该类的元素。

A space separate children from parent. Without the space you are telling the CSS to find an element with that class instead.

谁把谁当真 2024-12-07 16:37:46

是:

  • 第一个案例的目标是 td 的子元素,具有“someclass”类,
  • 第二个案例的目标是具有“someclass”类的 td

Yes:

  • first case targets an child element of your td, having class "someclass"
  • second case targets td having class "someclass"
蝶舞 2024-12-07 16:37:46

空间确实有所作为。第一个选择器选择类 someClass 的任何元素,该元素是 myTabletrtd 的子元素。

第二个选择任何具有 someClass 类的 td,该类是 myTabletr 的子级

The space does make a difference. The first selector selects any element with the class someClass that is a child of a td in a tr in myTable.

The second selects any td with the class someClass that is a child of a tr in myTable

过期以后 2024-12-07 16:37:46

td 后面的空格确实有所不同

css:

#myTable tr td.someClass{ color:brown; }
#myTable tr td .someClass{ color:red; }

html:

<table id="myTable">
<tr>
    <td class="someClass">
        cofee
        <span class="someClass">
            wine
        </span>
    </td>
</tr>

咖啡会呈现棕色,而酒红色。

The space after the tddoes make a difference

css:

#myTable tr td.someClass{ color:brown; }
#myTable tr td .someClass{ color:red; }

html:

<table id="myTable">
<tr>
    <td class="someClass">
        cofee
        <span class="someClass">
            wine
        </span>
    </td>
</tr>

cofee will appear brown, and wine red.

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