javascript - 使用 Object.setPrototype() 修补同一文档中的混合 HTML 和 XML 元素
问题描述
假设我将一个元素从 an 移动XMLDocument
到 an HTMLDocument
:
// create XML doc
const xmlDoc = document.implementation.createDocument(null, 'my-root');
// reference the XML element
const myRoot = xmlDoc.childNodes[0];
// move to this HTML doc
document.body.appendChild(myRoot);
myRoot instanceof Element; // true
myRoot instanceof HTMLElement; // false
myRoot.setAttribute('style', '...'); // doesn't render styles
// try to patch it
Object.getPrototypeOf(myRoot); // Element
Object.setPrototypeOf(myRoot, HTMLUnknownElement.prototype);
Object.getPrototypeOf(myRoot); // HTMLUnknownElement
myRoot.style; // error, illegal invocation
myRoot.setAttribute('style', '...'); // still doesn't render styles
由于myRoot
是一个Node
,它参与了 DOM 树,可以查询等等。它还具有所有与属性相关的功能Element
。
但:
- 它缺少所有与样式相关的功能
HTMLElement
- 它不能升级为自定义元素
- 在序列化/反序列化回合之后,它将成为
HTMLUnknownElement
setPrototypeOf
起初似乎有效,但style
财产失败- 即使在设置原型之后,任何
style
属性更改都不会呈现
为什么style
设置原型后访问会中断?为什么设置原型后样式不起作用?
解决方案
推荐阅读
- here-api - Here.com API 返回错误的国家代码 NCY
- java - 在 Eclipse 中运行应用程序时设置 ClassPath 命令行参数
- reactjs - 如何从 React js 中的 s3 存储桶中获取 5 个最新文件?
- php - 如何使用 Laravel Socialite 登录用户(LinkedIn)
- java - 如何使用 JavaEE eclipse 将我的 React Native 应用程序连接到 MySQL 数据库?
- javascript - 如何修复硬刷新/缓存干净刷新中没有加载的数据
- python - 如何使用 Python 中的数据集使用 PSO(粒子群优化)实现聚类?
- sql - 在使用“CASE”时,是否有任何功能可以减少我的查询大小
- react-native - 是否可以从 AsyncStorage 中删除所有项目
- java - 找不到数据源 JNDI