Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onclick密码警报,同时还有onclick打印_Javascript_Jquery_Html - Fatal编程技术网

Javascript onclick密码警报,同时还有onclick打印

Javascript onclick密码警报,同时还有onclick打印,javascript,jquery,html,Javascript,Jquery,Html,我有一个问题,我有一个打印按钮,我想在打印窗口弹出前提示/提醒密码 这是我的护照 我可以打开提示,但我想输入密码,然后我想打开打印窗口 请帮助更改脚本,如下所示: document.getElementById("btnPrint").onclick = function () { var password; var pass1="cool"; password=prompt('Please enter your password to view this page!',' ');

我有一个问题,我有一个打印按钮,我想在打印窗口弹出前提示/提醒密码

这是我的护照

我可以打开提示,但我想输入密码,然后我想打开打印窗口


请帮助更改脚本,如下所示:

document.getElementById("btnPrint").onclick = function () {
var password;

    var pass1="cool";

password=prompt('Please enter your password to view this page!',' ');

if (password==pass1)
{ 
  printElement(document.getElementById("printThis"));
} //Close if here
} // Close your btnPrint click function here

function printElement(elem) {
var domClone = elem.cloneNode(true);

var $printSection = document.getElementById("printSection");

if (!$printSection) {
    var $printSection = document.createElement("div");
    $printSection.id = "printSection";
    document.body.appendChild($printSection);
}

$printSection.innerHTML = "";
$printSection.appendChild(domClone);
window.print();
}

这里是更新的

有逻辑错误

说明:

/* $printSection is declared */
var $printSection = document.getElementById("printSection");
/* if $printSection isnt exists.. */
if (!$printSection) {
    /* ERROR : make new local variable that named as $printSection*/
    var $printSection = document.createElement("div");
    $printSection.id = "printSection";
    document.body.appendChild($printSection);
}
/* this code is the same as null.appendChild(domClone) */
$printSection.appendChild(domClone);
所以这个代码应该是

var $printSection = document.getElementById("printSection");
if (!$printSection) {
    /* like this*/
    $printSection = document.createElement("div");
    $printSection.id = "printSection";
    document.body.appendChild($printSection);
}
$printSection.appendChild(domClone);

提示中不能有
type='password'
。使用
absolute
positioned弹出窗口来实现这一点。brO打印窗口不在,它来了。检查这个屏幕截图。我已经登录了Firefox/Chrome/IE11。进入pass1打印窗口后,其工作(如果您将密码设置为“酷”)不起作用是的,不会出现。As
pass1
是变量,但其值为'cool'。所以当你输入'cool'作为密码时,如果我想要输入类型的密码,比如想要隐藏密码,我该怎么做
var $printSection = document.getElementById("printSection");
if (!$printSection) {
    /* like this*/
    $printSection = document.createElement("div");
    $printSection.id = "printSection";
    document.body.appendChild($printSection);
}
$printSection.appendChild(domClone);