单击按钮使文本显示/消失 [英] Make text appear/disappear on button click

查看:73
本文介绍了单击按钮使文本显示/消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使按钮使用'none'替代隐藏的文本,而使用'block'替代显示的文本.我尝试了下面的方法,但是没有用

I'm trying to make the button alternate the text from hidden using 'none' to appear using 'block'. I tried the below but it did not work

<p id='demo' style = 'display: none'>Hello Javascript</p>     
<button type='button' onclick="document.getElementById('demo').style.display='block'" >click me</ button>

我想将事件侦听器转换为Javascript并从那里运行.

I want to convert event listener to Javascript and run from there.

推荐答案

尝试一下,希望对您有所帮助.谢谢

Try this, I hope it'll help you out. Thanks

function toggleText() {
  var text = document.getElementById("demo");
  if (text.style.display === "none") {
    text.style.display = "block";
  } else {
    text.style.display = "none";
  }
}

<p id='demo' style='display: none'>Hello Javascript</p>     
<button type='button' onclick="toggleText()">Click me</button>

这篇关于单击按钮使文本显示/消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆