在firefox浏览器控制台上,我可以访问<body></body>容器,如下所示。
> window
[object Window]
> documentObject = window["document"];
[object HTMLDocument]
> documentObject["body"]
[object HTMLBodyElement]请告诉我,我如何访问<head></head>容器?
注意:请不要推荐getElementByTagName的方法。我正试图像上面那样使用字典语法进行访问。
发布于 2015-06-05 07:02:29
你试过document.head了吗?您可以访问body,如:document.body。您也可以这样做:window.document.head和window.document.body,但是添加window不会改变任何事情。前者和后者是相同的。
您还可以使用computed member access运算符:
var head = document['head']; // Same as: window['document']['head']
var body = document['body']; // Same as: window['document']['body']发布于 2015-06-05 07:00:22
可以使用getElementsByTagName()访问head元素:
var x = document.getElementsByTagName("HEAD")[0].innerHTML;发布于 2015-06-05 07:10:06
如果您查看jQuery源代码,您会发现以下内容:
var head = document.head ||
document.getElementsByTagName("head")[0] ||
document.documentElement;https://stackoverflow.com/questions/30660246
复制相似问题