Reactjs 使用ref获取react中的iframe元素
我有一个包含iframe的组件,我想在react中访问它的内容,我使用ref处理iframe,如何从iframe获取所有锚标记 这是我的密码:Reactjs 使用ref获取react中的iframe元素,reactjs,iframe,use-ref,Reactjs,Iframe,Use Ref,我有一个包含iframe的组件,我想在react中访问它的内容,我使用ref处理iframe,如何从iframe获取所有锚标记 这是我的密码: const GridGenerator = () => { const [loading, setLoading] = useState(true); const gridIframe = useRef(null); function handleIframe() { setLoading(false);
const GridGenerator = () => {
const [loading, setLoading] = useState(true);
const gridIframe = useRef(null);
function handleIframe() {
setLoading(false);
const iframeItem = gridIframe.current;
const anchors = iframeItem.contentWindow.getElementsByTagName("a");
}
return (
<div>
{loading ? <div className={styles.loader} /> : null}
<iframe
title="Grid Generator"
ref={gridIframe}
src="https://collectearth.users.earthengine.app/view/collect-earth-grid-generator"
width="100%"
height="1000px"
frameBorder={0}
onLoad={handleIframe}
/>
<Link to={routes.HOME}>Go Back</Link>
</div>
);
};
它返回iframe标记,但这一行不能正常工作
const anchors = iframeItem.contentWindow.getElementsByTagName("a");
有解决办法吗?谢谢控制台中是否有错误?您可能没有访问iframe的DOM的权限。我得到了一个:react DOM.development.js:328 Uncaught DOMException:阻止带有原点“file://”的帧访问跨原点帧。这就是我的想法。除非您也拥有iframe的网站,否则您无权访问其他域中iframe的DOM。
const anchors = iframeItem.contentWindow.getElementsByTagName("a");