【发布时间】:2014-01-23 04:26:59
【问题描述】:
在我的应用程序中,最初这些代码有效:
var htm = "<div> My content </div>";
$("#divprint").html(htm);
window.setTimeout('window.print()', 4000);
但是,当我尝试将其包装在 if 语句中时,它不会打印:
var canprint= $.cookie("actionprint");
alert("I am outside " + canprint);
if(canprint == null){
alert("I am inside");
var htm = "<div> My content </div>";
window.setTimeout('window.print()', 4000);
}
当我运行此代码时,我只收到第一个警报:“我在 null 之外”
由于 javascript 的范围是函数级别,我想知道,如果失败了,为什么会这样?
【问题讨论】:
-
我在您的代码中没有看到任何函数定义。顺便说一句,
console.log(typeof canprint, canprint); -
是不是忘记在变量htm的声明中加双引号了?
-
您的代码是否像您的帖子一样缺少右引号?
-
$.cookie()对于不存在的道具返回undefined。 -
@BlackSheep - 我认为这是真正的原因
标签: javascript jquery html