- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以得到 nodeType
来自 <a>
这样的节点或 <div>
在 Chrome devtools 中,但我无法获得 nodeType
的一个属性。我试过这样:
document.getElementByTagName('a')[0].href.nodeType
DOM 标准说属性也是一种节点。为什么不能得到它的nodeType
?
最佳答案
...but I can't get the nodeType of an attribute. I've tried like this:
document.getElementByTagName('a')[0].href.nodeType
当您访问元素上的 .href
时,您没有使用属性节点(不是直接),您使用的是属性的反射属性 (它在幕后访问属性节点)。该属性是返回字符串的访问器属性。字符串没有 nodeType
属性。 (您可以在 HTML 规范中找到它,从 the a
element 开始,它告诉您它是一个 HTMLAnchorElement
,其中包括 HTMLHyperlinkElementUtils
,其中有 href
定义了 href
访问器的行为属性(property)。)
要直接访问属性节点,您可以使用 attributes
收藏或getAttributeNode
:
const example = document.getElementsByTagName("a")[0];
const node = example.attributes[0];
console.log(`node.nodeType = ${node.nodeType}`);
console.log(`node.nodeName = ${node.nodeName}`);
console.log(`node.nodeValue = ${node.nodeValue}`);
<a href="http://example.com">example</a>
在您提出的评论中:
But how do you know that?Does the Dom standard says that in any line?
老实说,我在大约 24 年的时间里学习了这些东西,并且不记得我是什么时候第一次了解到虽然属性是 DOM 的一部分,但它们不是节点树的一部分,它们是分开保存的作为 attributes
NamedNodeMap
。该信息在定义 node tree 的 DOM 规范中。 (注意它没有提到属性节点),但是......你必须寻找它才能捕捉到我认为的。 :-) 不过,如果您对 DOM 和 HTML 规范了解的时间足够长,您就会学会这些东西。
关于javascript - 为什么我不能像 DOM 标准所说的那样获取节点属性的 nodeType 属性也是一个属性节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57840336/
我在 Swift 中处理这个问题有一段时间了,尝试了类型删除和各种东西,但都无济于事;这个问题似乎是常识(至少对我而言)。如何在 Swift 中模拟抽象类?换句话说,如何使这项工作: import F
我试图找到一种方法来检测超链接是否是 的第一个节点. 例如,如果我有: hyperlink Text text text text... Text text text text h
我开始探索如何使用 Javascript 操作 DOM。我从非常基础的开始,但我立即陷入困境。 所以从W3我发现HTML中有3种类型的节点: 元素 属性 文字 以此片段为例My paragraph我想
我使用 jQUery UI Position 插件:http://jqueryui.com/position/将我的图标放置在网页上。选择器从数据库中获取,并使用 PHP 在 $myselector
function prev( node ){ do { elem = elem.previousSibling; } while( elem && elem.nodeTy
我正在尝试使用 jquery 查找页面上的所有文本节点。但无论我选择什么元素,nodeType 始终为 1: $.each($('*'), function(index, el) { console.
尝试安装集群时,我收到此错误: Cluster manifest validation failed with exception System.ArgumentException: NodeType
我有一个 XML 文件,我需要从中读取一些文本值。我在使用 reader.NodeType = XMLNodeType.Text 时遇到问题,无法理解什么构成了 NodeType.Text 的节点。
本文整理了Java中xdi2.core.features.nodetypes.XdiCommonRoot类的一些代码示例,展示了XdiCommonRoot类的具体用法。这些代码示例主要来源于Githu
本文整理了Java中xdi2.core.features.nodetypes.XdiEntity类的一些代码示例,展示了XdiEntity类的具体用法。这些代码示例主要来源于Github/Stacko
我在 IE (6,7,8) 中的网站上遇到此问题: ‘nodeType’ is null or not an object 错误涉及“f.nodeType”属性。基本上 f 是未定义的,所以问题是之前
基于 this ,看起来每次我想获取节点元素名称、文本值或其他任何内容时,我都需要使用 switch-case。 那么我是否必须使用这样的东西: while (reader.Read()) {
我想获取nodeType,然后将其与文本节点或元素节点的位置进行比较。 $(function(){ var mm= $('.jj') alert(mm.nodeValue)
nodeName 属性含有某个节点的名称。 元素节点的 nodeName 是标签名称 属性节点的 nodeName 是属性名称 文本节点的 nodeName 永远是 #text 文档节点的
我有这个小注册表 Please Register Your Account
我有一个带有文本的输入框。我想在用户按 Enter 时启动一个功能。该函数如下: si = $("#maincopy_search_input").val(); $("#search_input").
在 jquery 中有一个名为 'isArrayLike' 的函数,它被许多函数使用,例如 $.each function isArraylike( obj ) { var length = obj.
我可以得到 nodeType来自 这样的节点或 在 Chrome devtools 中,但我无法获得 nodeType的一个属性。我试过这样: document.getElementByTagNam
我正在尝试为学校做一个 knockout.js 项目,我需要将一个可观察数组绑定(bind)到一个选择下拉列表。以下代码不起作用,并在 applyBindings 行上给出此错误: Uncaught
本文整理了Java中xdi2.core.features.nodetypes.XdiCommonRoot.getSelfPeerRoot()方法的一些代码示例,展示了XdiCommonRoot.get
我是一名优秀的程序员,十分优秀!