X6 icon indicating copy to clipboard operation
X6 copied to clipboard

react 节点下 foreignObject 可以不使用body标签名?

Open 52flutter opened this issue 2 years ago • 8 comments

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

image

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 avatar Dec 14 '23 03:12 52flutter

👋 @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.

x6-bot[bot] avatar Dec 14 '23 03:12 x6-bot[bot]

可以使用 子元素选择器 > , 限定是html子元素的那个body(html > body) https://developer.mozilla.org/zh-CN/docs/Web/CSS/Child_combinator

image image image

XueMeijing avatar Dec 15 '23 02:12 XueMeijing

我知道这样可以 很多库里面写了$('body'),layui就有

52flutter avatar Dec 16 '23 02:12 52flutter

一个一个改很麻烦 而且不好升级

52flutter avatar Dec 16 '23 02:12 52flutter

https://github.com/antvis/X6/blob/master/packages/x6-vue-shape/src/node.ts#L26

可以试试不传primer参数,或者传一个null进去,应该就没有body元素了

lloydzhou avatar Dec 25 '23 03:12 lloydzhou

为什么这里可以在foreignObject下使用body标签而没有警告,我在项目下使用的其他组件被这个错误的body误导出现错误行为,我尝试在自己的组件下新建一个body屏蔽这个行为,但是 React 给我警告:<body> cannot appear as a child of <foreignObject>.

Cedrus-P avatar Jan 07 '24 06:01 Cedrus-P

为什么这里可以在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);
}, []);

XueMeijing avatar Jan 08 '24 02:01 XueMeijing

我也遇到这个问题了,请问解决了吗

yjz321 avatar Jan 12 '24 09:01 yjz321