使用javascript使按钮或链接仅在函数调用时可见

使用javascript使按钮或链接仅在函数调用时可见,javascript,Javascript,我想添加一个按钮或链接以显示在浏览器弹出窗口中,该窗口发出servlet请求以获取数据。但是当出现错误时,处理此问题的javascript应该显示该窗口上的链接或按钮。否则,我们将继续隐藏 function showErrorOnStart() { document.getElementById("video").innerHTML = "<p>This file is of unsupported type. Please download t

我想添加一个按钮或链接以显示在浏览器弹出窗口中,该窗口发出servlet请求以获取数据。但是当出现错误时,处理此问题的javascript应该显示该窗口上的链接或按钮。否则,我们将继续隐藏

 function showErrorOnStart()
    {
    document.getElementById("video").innerHTML = "<p>This file is of 
         unsupported type. Please download the file</p>";
    aud.style.visibility = "hidden";
    vid.style.display = "none";

    function myFunction() {
      var x = document.getElementById("buttonLink");
      if (x.style.display === "none") {
        x.style.display = "block";
      } else {
        x.style.display = "none";
      }
   }
   }


    <button id="buttonLink" type="button" 
         style="display:none;" onclick="myFunction()">DownLoad</button>
函数showErrorOnStart()
{
document.getElementById(“视频”).innerHTML=“此文件是
不支持的类型。请下载文件

”; aud.style.visibility=“隐藏”; vid.style.display=“无”; 函数myFunction(){ var x=document.getElementById(“buttonLink”); 如果(x.style.display==“无”){ x、 style.display=“block”; }否则{ x、 style.display=“无”; } } } 下载

我不知道我在做什么,希望有人能指导我正确地完成这项工作。基本上,当出现错误时会调用此函数并显示适当的消息,但这次我想包括一个链接/按钮,该窗口将显示该链接/按钮,用户可以单击该链接/按钮并返回视频流。

PROTIP:正如我经常对新手说的,缩进是必须的。您的代码缩进不良,类似的情况可能会导致代码误解,从而导致错误

据我所知,您的代码很好,但当我清理缩进时,您在另一个函数中定义了一个函数:

function showErrorOnStart() {
    document.getElementById("video").innerHTML = "<p>This file is of unsupported type. Please download the file</p>";
    aud.style.visibility = "hidden";
    vid.style.display = "none";
    function myFunction() {
        var x = document.getElementById("buttonLink");
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
}

有了它,它应该可以按预期工作(只要定义函数的位置是全局范围,而不是在另一个函数中)。

这个问题的格式非常糟糕。注意细节,比如你留在外面的支架。编程时必须关注小细节。一个简单的角色会让你发疯。也许你应该多读或多写或多重读你写的东西,但如果你不想在StackOverflow注册的这8年里遇到所有问题,就去做吧;对不起,我应该更清楚。
function showErrorOnStart() {
    document.getElementById("video").innerHTML = "<p>This file is of unsupported type. Please download the file</p>";
    aud.style.visibility = "hidden";
    vid.style.display = "none";
}
function myFunction() {
    var x = document.getElementById("buttonLink");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}