Javascript REACT Uncaught TypeError:无法读取属性';加载';空的
这是Javascript REACT Uncaught TypeError:无法读取属性';加载';空的,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,这是iframeloader代码,它在类组件中工作良好 我尝试处理iframe,因此iframe给我null,但我需要处理iframe。我怎样才能到达呢? 也许我需要更改加载程序代码中的某些内容 HTMLIFrameElement.prototype.load = function(url, callback) { const iframe = this; try { iframe.src = url + '?rnd=' + Math.rand
iframe
loader代码,它在类组件中工作良好
我尝试处理iframe,因此iframe给我null,但我需要处理iframe。我怎样才能到达呢?
也许我需要更改加载程序代码中的某些内容
HTMLIFrameElement.prototype.load = function(url, callback) {
const iframe = this;
try {
iframe.src =
url +
'?rnd=' +
Math.random()
.toString()
.substring(2);
} catch (err) {
if (!callback) {
return new Promise((resolve, reject) => {
reject(err);
});
} else {
callback(err);
}
}
const maxTime = 60000;
const interval = 200;
let timerCount = 0;
if (!callback) {
return new Promise((resolve, reject) => {
const timer = setInterval(() => {
if (!iframe) return clearInterval(timer);
timerCount++;
if (
iframe.contentDocument &&
iframe.contentDocument.readyState === 'complete'
) {
clearInterval(timer);
resolve();
} else if (timerCount * interval > maxTime) {
reject(new Error('Iframe load failed'));
}
}, interval);
});
} else {
const timer = setInterval(() => {
if (!iframe) return clearInterval(timer);
timerCount++;
if (
iframe.contentDocument &&
iframe.contentDocument.readyState === 'complete'
) {
clearInterval(timer);
callback();
} else if (timerCount * interval > maxTime) {
callback(new Error('Iframe load failed'));
}
}, interval);
}
};
可生成示例:我不知道您想做什么,这段代码中有很多错误,但下面是一个如何获取
iframe
引用的示例:
import React, { useEffect, useRef } from 'react';
import './helpers/iframeLoader.js';
const currentPage = 'index.html';
const Editor = () => {
const iframeRef = useRef();
useEffect(() => {
iframeRef.current.load(currentPage, () => {
const body = iframeRef.current.contentDocument.body;
body.childNodes.forEach(node => {
console.log(node);
});
});
}, []);
return (
<iframe title="iframe" ref={iframeRef} src={currentPage} frameBorder="0" />
);
};
export default Editor;
import React,{useffect,useRef}来自“React”;
导入“./helpers/iframeLoader.js”;
const currentPage='index.html';
常量编辑器=()=>{
常量iframeRef=useRef();
useffect(()=>{
iframeRef.current.load(currentPage,()=>{
const body=iframeRef.current.contentDocument.body;
body.childNodes.forEach(节点=>{
console.log(节点);
});
});
}, []);
返回(
);
};
导出默认编辑器;
我不知道您想做什么,这段代码中有很多错误,但下面是一个如何获取
iframe
引用的示例:
import React, { useEffect, useRef } from 'react';
import './helpers/iframeLoader.js';
const currentPage = 'index.html';
const Editor = () => {
const iframeRef = useRef();
useEffect(() => {
iframeRef.current.load(currentPage, () => {
const body = iframeRef.current.contentDocument.body;
body.childNodes.forEach(node => {
console.log(node);
});
});
}, []);
return (
<iframe title="iframe" ref={iframeRef} src={currentPage} frameBorder="0" />
);
};
export default Editor;
import React,{useffect,useRef}来自“React”;
导入“./helpers/iframeLoader.js”;
const currentPage='index.html';
常量编辑器=()=>{
常量iframeRef=useRef();
useffect(()=>{
iframeRef.current.load(currentPage,()=>{
const body=iframeRef.current.contentDocument.body;
body.childNodes.forEach(节点=>{
console.log(节点);
});
});
}, []);
返回(
);
};
导出默认编辑器;