Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

javascript - How do I use the CSS display property on clicking a button?

I am new to JavaScript, and I've been trying to use a button to show a particular element. I need to be able to clear some content of the page and display new content on the click of a button. I cant change pages, and need to stay on the same page. I am trying to change the display property of the element I want to display.

This is what I have tried:

//CSS//

<style>
    p {
      display: visible;
    }
    div {
      display: none;
    }
</style>
//HTML//

<body>
    <p id="textElem">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
    <button onclick="displayDiv()">View Form</button>
    <div id="hiddenElem">
      <form></form>
    </div>
<body>
//JS//
<script>
    const textElement = document.getElementById('textElem');
    cont hiddenElement = document.getElementById('hiddenElem');
    function displayDiv() {
  
    }
</script>

I do not know what the syntax is to reference CSS properties in Java Script. What I want to do is to change the display property of the paragraph to hidden and the display property of the div to visible when the button is clicked.

Could someone please help me out with this?

question from:https://stackoverflow.com/questions/65858304/how-do-i-use-the-css-display-property-on-clicking-a-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Meet the style object.

function displayDiv() {
    textElement.style.display = 'none';
    hiddenElement.style.display = 'block';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...