使用新的 jsPDF .html() 几乎直接从他们的文档中提取出来,但它仍然会导致一个空白页面:
结果在空白页:
function saveDoc() {
window.html2canvas = html2canvas
const doc = document.getElementById('doc')
if (doc) {
var pdf = new jsPDF('p', 'pt', 'a4')
pdf.html(doc.innerHTML, {
callback: function (pdf) {
pdf.save('DOC.pdf');
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
结果没有生成 PDF:
function saveDoc() {
window.html2canvas = html2canvas
const doc = document.getElementById('doc')
if (doc) {
var pdf = new jsPDF('p', 'pt', 'a4')
pdf.html(doc.innerHTML, {
function (pdf) {
pdf.save('DOC.pdf');
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
也会导致空白页:
function saveDoc() {
window.html2canvas = html2canvas
const doc = document.getElementById('doc')
if …Run Code Online (Sandbox Code Playgroud) 我有一个 React 组件,它似乎没有正确重置其初始状态。我正在使用 Object.assign() 来确保传递的道具具有所有字段(以改进可能未定义键的旧数据库条目)。
newProfile 是一个完整的配置文件(带有标题字段),而 legacyProfile 是添加标题字段之前的条目。为什么/如何传递 legacyProfile 的组件以某种方式保留来自另一个实例的一些数据(传递 newProfile 的组件),以及如何防止这种情况发生以确保我总是从一个新的 profileInitialState 开始?
const profileInitialState = {
name: '',
title: '',
headline: '',
bio: ''
}
class Bio extends React.Component {
constructor(props) {
super(props);
this.state = {
editingProfile: Object.assign(profileInitialState, this.props.profile)
}
}
render() {
const { profile } = this.props;
const { editingProfile } = this.state;
console.log(profile); // props profile has correct info
console.log(editingProfile); // state profile takes on old values
return (
<div>
<h1>{editingProfile.name}</h1>
<h2>{editingProfile.title}</h2>
<h3>{editingProfile.headline}</h3>
<p>{editingProfile.bio}</p> …Run Code Online (Sandbox Code Playgroud)当 iframe 的内容加载时,我需要触发一个 javascript 函数。我看到很多(过时的?)使用 iframe onload 事件的答案,这似乎根本没有触发(在 Chrome 中)。
根本不触发(至少在 Chrome 中):
<iframe id="target-iframe" src="SRC_URL" onload="iframeLoaded()"></iframe>
Run Code Online (Sandbox Code Playgroud)
在内容准备好之前为 iframe 的 readyState 设置一个监听器:
function listenForIframe() {
const iframe = document.getElementById("target-iframe");
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
// Check if loading is complete
if (iframeDoc.readyState == 'complete') {
iframe.contentWindow.onload = function(){
// console.log('loaded')
};
iframeLoaded();
return;
}
window.setTimeout(listenForIframe, 100);
}
Run Code Online (Sandbox Code Playgroud)
我当前的用例是使用 iframe-resizer 并收到以下错误消息,因为 iframe 内容尚不存在。只是想在这里保持一个干净的控制台!
iframeResizer.js:791 Failed to execute
'postMessage'on'DOMWindow': The target origin('http://localhost:3000')does not match the receiver …