点击按钮,一切都消失了

时间:2018-12-17 13:00:05

标签: javascript html buttonclick

我一直在尝试编写一个模拟此示例的代码(下面的代码)。

function closeMe(){

   		x=document.getElementById("demo");

   		x.style.display="none";

	}

function openMe(){

   		x=document.getElementById("demo");

   		 x.style.display="block";

	}
	<h1>Changing the Style</h1>
	<p>JavaScript can change the style of an HTML element.</p>

	<button type="button" onclick="openMe()">Open!</button>
	<button type="button" onclick="closeMe()">Close!</button>

	<p id="demo">Extra details...You can open and close this paragraph using the buttons above.</p>

不幸的是,在下面的我自己的代码中,当我单击“打开”按钮时,所有东西都消失了。我不知道为什么。

function open() {
  x = document.getElementById("demo");
  x.style.display = "block";
}

function close() {
  x = document.getElementById("demo");
  x.style.display = "none";
}
<p id="demo">Click me to change my HTML content (innerHTML).</p>

<p> this shouldnt disappear</p>

<button onclick="open()">open button</button>

<button onclick="close()">close button</button>

有人可以给我一些建议吗?

6 个答案:

答案 0 :(得分:3)

将显示设置为“阻止”时,请在“阻止”周围加上引号

def createWatchCustomObjectsCalls() = {
    client.getHttpClient.setReadTimeout(0, TimeUnit.SECONDS)
    val watchCalls: Watch[V1Namespace]  = Watch.createWatch(client,
      apiInstance.listNamespacedCustomObjectCall(crdGroup, crdVersion,  crdNamespace, crdPlural, "true",  null, null, true,null,  null),
      new TypeToken[Watch.Response[V1Namespace]]{}.getType)
    watchCalls
  }

 override def beforeAll(): Unit = {
    val creationResourcePath = Source.getClass.getResource("/" + httpServerScriptName).getPath
    val serverStartupProcessBuilder = Seq("sh", creationResourcePath, "&") #> Console.out
    serverStartupProcessBuilder.run()

    val body = convertYamlToJson()
    val sparkAppCreation = apiInstance.createNamespacedCustomObject(crdGroup, crdVersion, crdNamespace, crdPlural, body,"true")
    println(sparkAppCreation)
}

答案 1 :(得分:2)

您的函数名称已被使用。保持代码不变,除了可以测试的函数名称之外,所有功能均按预期工作。

  function open123(){
    let x=document.getElementById("demo");
    x.style.display="block";
  }

  function close123(){
    let x=document.getElementById("demo");
    x.style.display="none";
  }
<p id="demo">Click me to change my HTML content (innerHTML).</p>

<p> this shouldnt disappear</p>

<button onclick="open123()">open button</button>

<button onclick="close123()">close button</button>

答案 2 :(得分:1)

所以这些功能都在窗口范围内,所以我叫window.function名称。 我还在函数中声明了变量,因为它引发了一些JavaScript错误。

现在通常不需要这些,但是我感觉jsfiddle使用“ use strict”;这可能会迫使这些区别。但是我愿意接受其他建议。

希望这会有所帮助。

function open() { 
  var x = document.getElementById("demo");
  x.style.display = "block";
}

function close() {
  var x = document.getElementById("demo");
  x.style.display = "none";
}
<!DOCTYPE html>
<html>

<body>

  <p id="demo">Click me to change my HTML content (innerHTML).</p>

  <p> this shouldnt disappear</p>

  <button onclick="window.open();">open button</button>

  <button onclick="window.close();">close button</button>
</body>

</html>


答案 3 :(得分:1)

打开和关闭是保留关键字。您需要重命名关闭和打开方法。

open()方法打开一个新的浏览器窗口。

close()方法关闭窗口。

答案 4 :(得分:1)

您需要在函数中使用大写字母,并且样式必须在引号之间;

尝试使用干净的代码:)

function Open(){
        x = document.getElementById("demo");
        x.style.display = "block";
    }

        function Close(){
        x = document.getElementById("demo");
        x.style.display = "none";
    }

答案 5 :(得分:1)

open()和close()是保留关键字。 请更改函数名称,例如openOne()和closeOne(),这样就可以了。

相关问题