我有一个小问题.在从服务请求数据后,我得到了一个iframe代码作为响应.
<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>
Run Code Online (Sandbox Code Playgroud)
我想把它作为道具传递到我的模态组件并显示它,但是当我只是{this.props.iframe}在渲染函数中它显然将它显示为一个字符串.
将其显示为html的基本方式是什么?
Dha*_*tel 31
如果您不想使用dangerouslySetInnerHTML,那么您可以使用下面提到的解决方案
var Iframe = React.createClass({
render: function() {
return(
<div>
<iframe src={this.props.src} height={this.props.height} width={this.props.width}/>
</div>
)
}
});
ReactDOM.render(
<Iframe src="http://plnkr.co/" height="500" width="500"/>,
document.getElementById('example')
);
Run Code Online (Sandbox Code Playgroud)
这里现场演示可用Demo
Ale*_* T. 26
你可以dangerouslySetInnerHTML像这样使用财产
const Component = React.createClass({
iframe: function () {
return {
__html: this.props.iframe
}
},
render: function() {
return (
<div>
<div dangerouslySetInnerHTML={ this.iframe() } />
</div>
);
}
});
const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>';
ReactDOM.render(
<Component iframe={iframe} />,
document.getElementById('container')
);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>Run Code Online (Sandbox Code Playgroud)
另外,你可以复制字符串中的所有属性(基于问题,你从服务器得到iframe作为字符串),其中包含<iframe>标签并将其传递给新<iframe>标签,就像那样
/**
* getAttrs
* returns all attributes from TAG string
* @return Object
*/
const getAttrs = (iframeTag) => {
var doc = document.createElement('div');
doc.innerHTML = iframeTag;
const iframe = doc.getElementsByTagName('iframe')[0];
return [].slice
.call(iframe.attributes)
.reduce((attrs, element) => {
attrs[element.name] = element.value;
return attrs;
}, {});
}
const Component = React.createClass({
render: function() {
return (
<div>
<iframe {...getAttrs(this.props.iframe) } />
</div>
);
}
});
const iframe = '<iframe src="https://www.example.com/show?data..." width="540" height="450"></iframe>';
ReactDOM.render(
<Component iframe={iframe} />,
document.getElementById('container')
);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"><div>Run Code Online (Sandbox Code Playgroud)
Hit*_*ahu 15
使用 ES6 你现在可以这样做
要加载的示例 Codepen URl
const iframe = '<iframe height="265" style="width: 100%;" scrolling="no" title="fx." src="//codepen.io/ycw/embed/JqwbQw/?height=265&theme-id=0&default-tab=js,result" frameborder="no" allowtransparency="true" allowfullscreen="true">See the Pen <a href="https://codepen.io/ycw/pen/JqwbQw/">fx.</a> by ycw(<a href="https://codepen.io/ycw">@ycw</a>) on <a href="https://codepen.io">CodePen</a>.</iframe>';
Run Code Online (Sandbox Code Playgroud)
加载 Iframe 的功能组件
function Iframe(props) {
return (<div dangerouslySetInnerHTML={ {__html: props.iframe?props.iframe:""}} />);
}
Run Code Online (Sandbox Code Playgroud)
用法:
import React from "react";
import ReactDOM from "react-dom";
function App() {
return (
<div className="App">
<h1>Iframe Demo</h1>
<Iframe iframe={iframe} />,
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)
在 CodeSandbox 上编辑:
https://codesandbox.io/s/react-iframe-demo-g3vst
| 归档时间: |
|
| 查看次数: |
82038 次 |
| 最近记录: |