[英]Called Javascript Function Not Executing
我有一个名为highchartCheck的内部函数,该函数似乎没有在执行,我也不知道为什么。 执行打印介质查询检查后,即使在if块的末尾调用它,也不会记录highchartCheck记录。
if (window.matchMedia) {
var mq = window.matchMedia('print');
if (mq.matches) {
console.log("Print JS Executed");
function highchartCheck() {
console.log("check func exec");
if (document.getElementById('highcharts-0') && document.getElementById("highcharts-2") && document.getElementById("highcharts-4")) {
document.getElementById("highcharts-0").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
document.getElementById("highcharts-2").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
document.getElementById("highcharts-4").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
console.log("if exec");
} else {
setTimeout(highchartCheck, 250);
}
}
//Print Styling
$('#one').find("rect").css({"width": "3.6in !important";});
$('#two').find("rect").css({"width": "3.6in !important";});
$('#three').find("rect").css({"width": "3.6in !important";});
highchartCheck();
}
}
在运行函数之前,您只需要检查DOM是否已完全加载即可。
function doStuff() {
// Set Attributes
document.getElementById("highcharts-0").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
document.getElementById("highcharts-2").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
document.getElementById("highcharts-4").querySelectorAll("svg")[0].setAttribute("viewBox", "0 0 5.5in 3.6in");
//Print Styling
$('#one').find("rect").css({"width": "3.6in !important"});
$('#two').find("rect").css({"width": "3.6in !important"});
$('#three').find("rect").css({"width": "3.6in !important"});
}
if (window.matchMedia) {
var mq = window.matchMedia('print');
if (mq.matches) {
// Call the function until true
if (document.readyState == "complete" || document.readyState == "loaded") {
// document is already ready to go - do what you want
doStuff();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.