【问题标题】:select document root using jquery使用 jquery 选择文档根目录
【发布时间】:2012-07-24 20:49:31
【问题描述】:

我可以使用

选择文档的正文和 html 部分
$('body')

$('html')

分别,但是如何选择文档根目录?

【问题讨论】:

  • 为什么要这样做?您没有必须在所有事情上都使用 jQuery - 也可以使用 javascript。
  • 有趣的是$(document).html()在这里返回null(使用jQuery 1.71),而$(document).text()返回预期的结果。

标签: jquery html jquery-selectors document-root


【解决方案1】:

不确定你的意思,但要选择你做的文档

$(document);

要获取文档的内容,我猜你需要 documentElement,它与大多数环境中的 <html> 标记相同。

$(document.documentElement);

【讨论】:

  • document.documentElement 指向html 元素...只是说:)
  • 完美运行。谢谢adeno :)
  • 令人惊讶的“文档”不在字符串中。
【解决方案2】:

Document 接口继承自 Node,表示整个文档,例如 HTML 页面。尽管 Document 节点在概念上是文档的根,但它实际上并不是根 - 根节点是 Document 中的第一个 Element 节点,并由其 documentElement 属性表示。

您可以使用以下代码选择 documentElement:

var root = document.documentElement;

$(document.documentElement);

【讨论】:

    【解决方案3】:

    DOM 的根始终是 html 元素。
    您可以通过$('html')$(':root') 获得它。

    以下断言应始终为真:

    $('html')[0] === $(':root')[0]
    $(':root')[0] === document.documentElement
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多