作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以解释为什么在我将其 textContent
设置为某项后,StyleSheet
的 ownerNode
为 null
吗?
const style = document.createElement('style')
document.head.appendChild(style)
const sheet = style.sheet
// passes
console.assert(sheet.ownerNode !== null, 'ownerNode is null')
sheet.ownerNode.textContent = 'div { color: red }'
// fails
console.assert(sheet.ownerNode !== null, 'ownerNode is null')
最佳答案
因为通过完全替换节点的文本,您创建了一个新的替换样式表对象:
const style = document.createElement('style')
document.head.appendChild(style)
const sheet = style.sheet
sheet.ownerNode.textContent = 'div { color: red }'
console.log(style.sheet === sheet); // <===== false!
console.log(style.sheet.ownerNode === style); // <===== true
旧节点不再存在于 DOM 中,因此它没有 ownerNode
。
关于javascript - 为什么StyleSheet 的ownerNode 插入后为null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682047/
这有效: Output "Cluster Group: ""$($Group.Name)"", Current Owner: $($Group.OwnerNode), Current State: $
我的网页中有多个 style 标签,我想操纵其中的 cssRules。如何从样式元素中获取 styleSheet 中的 document.styleSheets 对象。一种方法是扫描 document
我是一名优秀的程序员,十分优秀!