如何在CSS中的#document中选择元素? [英] How do I select elements within a #document in CSS?

查看:950
本文介绍了如何在CSS中的#document中选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在CSS中设置外部< body> 标记的背景图片:

I'm trying to set the background-image of the outer <body> tag in CSS:

<html>
 <body>
  ...
   #document
    <html>
     <body>
     </body>
    </html>
  ...
 </body>
</html>


但是,我甚至不能选择< body> 标记,而不影响其他标记。他们没有ID,当我尝试通过子选择器选择 #document 使事情复杂化(我甚至不知道它是什么)。如果我尝试 body html body {...} #document 就像一个障碍

However, I can't even seem to select one of the <body> tags without affecting the other. They have no IDs, and when I try selecting via child selectors that #document complicates things (I don't even know what it is). If I try something like body html body {...}, the #document acts like a barrier that I can't get past, so it doesn't select the inner body tag.

有关如何选择其中一个正文标签的任何想法?

Any ideas for how to select one of the body tags?

推荐答案

给定的列表看起来是一个DOM检查器视图。在这种情况下, #document 不引用id为document的任意元素 - 它指的是托管在< ; iframe> (请参阅 Node.nodeName )。你可以在Firefox和Chrome的DOM检查器中看到这一点;事实上,您甚至可以通过指向浏览器 data:text / html,< iframe> ,打开检查器并展开< iframe> 节点。 IE也显示嵌套树,但没有 #document 标记。

The given listing appears to be a DOM inspector view. In this case, #document doesn't refer to an arbitrary element with an id of "document" — it refers to the document hosted within an <iframe> (see Node.nodeName). You can see this in Firefox's and Chrome's DOM inspectors; in fact, you can even try it right now by pointing either browser to data:text/html,<iframe>, opening the inspector and expanding the <iframe> node. IE shows the nested tree as well, but without the #document marker.

如果你看到背景

选择器无法插入多个/嵌套的文档树;它们本地化到上下文文档。这意味着您不能在包含< iframe> < iframe> c $ c>。如果您需要在< iframe> 中的文档中的元素应用样式,则需要将CSS直接应用于该内部文档中,

Selectors cannot penetrate multiple/nested document trees; they are localized to the context document. This means that you cannot target elements in a document hosted within an <iframe> from within the document that contains that <iframe>. If you need to apply styles to elements in a document within an <iframe>, the CSS needs to be directly applied within that inner document, and the selectors written as though you were working with just that document.

在不太可能发生的事件中,给定的列表是实际的源代码标记,而不是DOM检查器视图, #document 实际上是一个id为document的任意元素,那么你的内部< html> < body> 元素已被注销,因为HTML DOM可能只包含一个< html> 和一个< body> 元素;这些将是你的外部,而不是内部。在这种情况下,您可以尝试选择 #document 并应用背景,但不保证它会正常工作。

In the somewhat less likely event that the given listing is actual source markup rather than a DOM inspector view, and #document is in fact an arbitrary element with an id of "document", then your inner <html> and <body> elements have been written off, as an HTML DOM may only contain exactly one <html> and one <body> element at a time; these would be your outer ones, not the inner ones. In that case, you could try selecting #document and applying the background to that instead, but no guarantees that it'll work correctly.

这篇关于如何在CSS中的#document中选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆