javascript - 为什么我的 JavaScript 可以在这里运行,但不能在我的本地主机或 JSFiddle 中运行?

标签 javascript html function

我正在尝试创建一个动态网页,其中每个按钮都显示一个特定的 <div>页面同时隐藏 <div> 的其余部分页面。

我将它部署在此处、我的本地主机和 jsfiddle 上,但不知为何它无法在 jsfiddle 和我的本地主机上正常运行,但它可以在此处运行。

我不太清楚为什么会这样?

我的 HTML:

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>
<button onClick="myFunction2()">Try it</button>
<button onClick="myFunction3()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<div id="div2" style="display: none;">
Div2
</div>

<div id="div3" style="display: none;">Div3</div>

我的 CSS:

#myDIV {
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: lightblue;
    margin-top: 20px;
}
#div2{
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: purple;
    margin-top: 20px;
}

#div3{
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: yellow;
    margin-top: 20px;
}

我的 JavaScript:

function myFunction() {
    var x = document.getElementById("myDIV");
    var y = document.getElementById("div2");
    var z = document.getElementById("div3");
    if (x.style.display === "none") {
        x.style.display = "block";
    y.style.display = "none";
        z.style.display = "none";
    }
}

function myFunction2() {
    var x = document.getElementById("div2");
    var y = document.getElementById("myDIV");
    var z = document.getElementById("div3");
    if (x.style.display === "none") {
        x.style.display = "block";     
        y.style.display = "none";
        z.style.display = "none";
    }
}

function myFunction3() {
    var x = document.getElementById("div3");
    var y = document.getElementById("myDIV");
    var z = document.getElementById("div2");
    if (x.style.display === "none") {
        x.style.display = "block";     
        y.style.display = "none";
        z.style.display = "none";
    }
}

function myFunction() {
    var x = document.getElementById("myDIV");
    var y = document.getElementById("div2");
    var z = document.getElementById("div3");
    if (x.style.display === "none") {
        x.style.display = "block";
    y.style.display = "none";
        z.style.display = "none";
    }
}

function myFunction2() {
    var x = document.getElementById("div2");
    var y = document.getElementById("myDIV");
    var z = document.getElementById("div3");
    if (x.style.display === "none") {
        x.style.display = "block";     
        y.style.display = "none";
        z.style.display = "none";
    }
}

function myFunction3() {
    var x = document.getElementById("div3");
    var y = document.getElementById("myDIV");
    var z = document.getElementById("div2");
    if (x.style.display === "none") {
        x.style.display = "block";     
        y.style.display = "none";
        z.style.display = "none";
    }
}
#myDIV {
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: lightblue;
    margin-top: 20px;
}
#div2{
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: purple;
    margin-top: 20px;
}

#div3{
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: yellow;
    margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>
<button onClick="myFunction2()">Try it</button>
<button onClick="myFunction3()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<div id="div2" style="display: none;">
Div2
</div>

<div id="div3" style="display: none;">Div3</div>

编辑:

我的 JSfiddle 链接:https://jsfiddle.net/yvw4gwwh/3/

最佳答案

<body> 中将您的 Javascript 加载类型更改为 No wrap - ; 在这里试试:https://jsfiddle.net/yvw4gwwh/14/

enter image description here

关于javascript - 为什么我的 JavaScript 可以在这里运行,但不能在我的本地主机或 JSFiddle 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49500584/

相关文章:

javascript - 如何在 JavaScript 中关闭选项卡或重新打开弹出选项卡?

javascript - 使用 momentjs 或 Date() 获取 3 月份的 2020-02-29T00 :00:00. 000Z

javascript - 如何在 jquery 代码中使用它

javascript - 我如何根据 addEventListener 屁股触发器将两个几乎相同的 javascript 函数写成一个

c++ - 如何在函数中分配指向新对象的指针,而该对象在编辑后不会消失

java - 在 BigDecimal 除法中使用 MathContext 对象

javascript - 去混淆 Javascript 代码以使其再次可读

css - 将 Div 拉伸(stretch)到容器中的页面底部

python - 如何仅将该网站 HTML 表的第一列和 href 链接抓取到 pandas 数据框中?

c - 解决这个识别功能?