运行 函数在 document.write 里面 document.write 在 window.open 里面
Running function inside document.write which document.write inside window.open
function capture(){
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
screenshot.document.write("<center/><img id='jpeg' src='"+img+"'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='"+img+"'>Download</a>");
screenshot.document.write("<button id='myLink' onclick='popOut()'>Test</button>");
}
function popOut(){
alert("Clicked");
}
按钮 myLink 在 window.open 之后出现,如果我点击它 none 就会发生。我怎样才能让 alert("Clicked") 出现? document.write里面的onclick功能是不是被禁用了?
PS: 我试过把popOut()函数放在capture()函数上面,但还是不行。请帮忙。
弹出的onclick
中的popOut
window不是current中的popOut
window.两个 window 环境是分开的。
不过,您可以将它提供给其他 window,方法是将其添加到 capture
的末尾:
screenshot.popOut = popOut;
Live Example: (因为 Stack Snippets 不允许 window.open
)
document.querySelector('input').onclick = capture;
function capture(){
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
/* Commented out since I don't have your `img` variable
screenshot.document.write("<center/><img id='jpeg' src='"+img+"'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='"+img+"'>Download</a>");
*/
screenshot.document.write("<button id='myLink' onclick='popOut()'>Test</button>");
screenshot.popOut = popOut;
}
function popOut(){
alert("Clicked");
}
HTML:
<input type="button" value="Click me">
两个函数的作用域不同
function capture() {
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
screenshot.document.write("<center/><img id='jpeg' src='" + img + "'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='" + img + "'>Download</a>");
screenshot.document.write("<button id='myLink'>Test</button>");
screenshot.document.getElementById('myLink').onclick = popOut
}
演示:Fiddle
function capture(){
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
screenshot.document.write("<center/><img id='jpeg' src='"+img+"'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='"+img+"'>Download</a>");
screenshot.document.write("<button id='myLink' onclick='popOut()'>Test</button>");
}
function popOut(){
alert("Clicked");
}
按钮 myLink 在 window.open 之后出现,如果我点击它 none 就会发生。我怎样才能让 alert("Clicked") 出现? document.write里面的onclick功能是不是被禁用了?
PS: 我试过把popOut()函数放在capture()函数上面,但还是不行。请帮忙。
弹出的onclick
中的popOut
window不是current中的popOut
window.两个 window 环境是分开的。
不过,您可以将它提供给其他 window,方法是将其添加到 capture
的末尾:
screenshot.popOut = popOut;
Live Example: (因为 Stack Snippets 不允许 window.open
)
document.querySelector('input').onclick = capture;
function capture(){
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
/* Commented out since I don't have your `img` variable
screenshot.document.write("<center/><img id='jpeg' src='"+img+"'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='"+img+"'>Download</a>");
*/
screenshot.document.write("<button id='myLink' onclick='popOut()'>Test</button>");
screenshot.popOut = popOut;
}
function popOut(){
alert("Clicked");
}
HTML:
<input type="button" value="Click me">
两个函数的作用域不同
function capture() {
var screenshot = window.open("", "_blank", "menubar=2,titlebar=0,toolbar=0,width=" + 680 + ",height=" + 550);
screenshot.document.write("<center/><img id='jpeg' src='" + img + "'><br/>");
screenshot.document.write("<a download='vfr_capture.jpeg' href='" + img + "'>Download</a>");
screenshot.document.write("<button id='myLink'>Test</button>");
screenshot.document.getElementById('myLink').onclick = popOut
}
演示:Fiddle