react 节点下 foreignObject 可以不使用body标签名?
Describe the bug
在一些JQ项目和库里,会导致JQuery("body")选择多个节点,dom操作的时候会都触发
Your Example Website or App
https://stackoverflow.com/help/mcve.
Steps to Reproduce the Bug or Issue
渲染antv/x6 react元素 JQuery("body").append("
")Expected behavior
JQuery("body").append("
")只在document.body下Screenshots or Videos
No response
Platform
- OS: [e.g. macOS, Windows, Linux]
- Browser: [e.g. Chrome, Safari, Firefox]
- Version: [e.g. 2.11.1]
Additional context
No response
👋 @52flutter
Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. To help make it easier for us to investigate your issue, please follow the contributing guidelines. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.
可以使用 子元素选择器 > , 限定是html子元素的那个body(html > body) https://developer.mozilla.org/zh-CN/docs/Web/CSS/Child_combinator
我知道这样可以 很多库里面写了$('body'),layui就有
一个一个改很麻烦 而且不好升级
https://github.com/antvis/X6/blob/master/packages/x6-vue-shape/src/node.ts#L26
可以试试不传primer参数,或者传一个null进去,应该就没有body元素了
为什么这里可以在foreignObject下使用body标签而没有警告,我在项目下使用的其他组件被这个错误的body误导出现错误行为,我尝试在自己的组件下新建一个body屏蔽这个行为,但是 React 给我警告:<body> cannot appear as a child of <foreignObject>.
为什么这里可以在foreignObject下使用body标签而没有警告,我在项目下使用的其他组件被这个错误的body误导出现错误行为,我尝试在自己的组件下新建一个body屏蔽这个行为,但是 React 给我警告:
<body> cannot appear as a child of <foreignObject>.
看源码 使用的是createElementNS, 带命名空间的创建可能不一样,我试了下这样确实不报错,但是不知道为什么会创建两个 foreignObject 出来
useEffect(() => {
var container = document.querySelector(`.${id}`);
var fo = container.querySelector('foreignObject');
if (fo) {
return;
}
fo = document.createElementNS('http://www.w3.org/1999/xhtml', 'foreignObject');
const body = document.createElementNS('http://www.w3.org/1999/xhtml', 'body');
const content = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
fo.setAttribute('width', '120');
body.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
body.setAttribute('class', 'ceshiyixia');
body.style.width = '100%';
content.style.width = '100%';
body.appendChild(content);
fo.appendChild(body);
container.appendChild(fo);
}, []);
我也遇到这个问题了,请问解决了吗