如何隐藏此段落标签?

时间:2019-03-06 19:49:27

标签: javascript html

我对这一切都很陌生,但是我学得很快,因为我认为这很容易。我试图使一个按钮隐藏另一个按钮产生的<p>标签。如何使按钮(位于代码底部)使<p>标签消失?

<button type="button"
onclick="document.getElementById('date_button').innerHTML = Date()">
Click this button to display the current time!</button><br>
<p id="date_button"></p><br><br>
<button onclick="myFuction()">Click this to hide date.</button>

3 个答案:

答案 0 :(得分:1)

您必须定义被调用的函数并使用display:none隐藏段落

function myFuction() {
  document.getElementById('date_button').style.display = 'none'
}
<button type="button" onclick="document.getElementById('date_button').innerHTML = Date();document.getElementById('date_button').style.display='block'">
Click this button to display the current time!</button><br>
<p id="date_button"></p><br><br>
<button onclick="myFuction()">Click this to hide date.</button>

答案 1 :(得分:0)

尝试一下。我认为这可以帮助

function myFuction() {
    document.getElementById('date_button').style.display = 'none';
}

答案 2 :(得分:0)

您要重新显示它,因此需要将其innerHTML设置为''。如果您愿意,style.display = 'none'。它不会再显示<p>

function myFunction(){
  document.getElementById('date_button').innerHTML = '';
}
<button type="button"
onclick="document.getElementById('date_button').innerHTML = Date()">
Click this button to display the current time!</button><br>
<p id="date_button"></p><br><br>
<button onclick="myFunction()">Click this to hide date.</button>

相关问题