[英]How to acess name attribute of iframe in React.js
import React, { useState, useEffect } from 'react' import "./MainText.css" import "./App.css"; import FormatColorFillIcon from '@material-ui/icons/FormatColorFill'; import BorderColorIcon from '@material-ui/icons/BorderColor'; function MainText() { function enableEditMode() { textPart.document.designMode = 'On'; } useEffect(() => { enableEditMode(); }, []) return ( <div className="mainText"> <div className="mainText__second"> <iframe name="textPart" frameBorder="0" ></iframe> </div> </div> )
I have only shown the main part of the code where I am not getting my result.我只显示了我没有得到结果的代码的主要部分。 I want to access the name attribute of the iframe tag but it is showing me textPart is not defined.
我想访问 iframe 标记的名称属性,但它显示 textPart 未定义。 I also tried by putting id in the iframe and doing document.getElementById but it is also not working.
我还尝试将 id 放入 iframe 并执行 document.getElementById 但它也不起作用。 Help me in this.
在这方面帮助我。
I think it supposed to be like this我认为它应该是这样的
<iframe name={"textPart"} frameBorder="0" ></iframe>
You could use this to get element by name: document.getElementsByName('textPart')
您可以使用它按名称获取元素:
document.getElementsByName('textPart')
So in your case the enableEditMode code will be:因此,在您的情况下, enableEditMode 代码将是:
function enableEditMode() {
const textpart = document.getElementsByName('textPart')[0];
const iframeDocument= textpart.contentDocument || textpart.contentWindow.document;
iframeDocument.designMode = "on";
}
function MainText() { function MainText() {
function enableEditMode(textPart) {
if(!textPart){
return;} // check for iframe content
textPart.contentWindow.addEventListener("load", () => {
textPart.document.designMode = 'On'; // check content iframe when iframe loaded
});
}
useEffect(() => {
enableEditMode();
}, [])
return (
<div className="mainText">
<div className="mainText__second">
<iframe ref={this.enableEditMode} name="textPart" frameBorder="0" >/iframe>
</div>
</div>
);
Have u tried ref to change iframe content?您是否尝试过参考更改 iframe 内容?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.