简体   繁体   中英

How do I change an element's text through JavaScript?

<section id="section1" data-nav="Section 1" class="your-active-class">
      <div class="landing__container">
        <h2>Section 1</h2>
        <p>Lorem ipsum dolor sit amet</p>
        <p>Aliquam a convallis justo.</p>
      </div>
    </section>

I have tried textContent and innerHTML, I tried to get element by ID, class and tag but nothing is working. I am pretty sure I am missing something. How could I change the paragraphs inside the headings, and the headings themselves as well?

Try this code, paste your paragraph's id attribute inside the getElementById thing.

var paragraph = document.getElementById("id-of-your-paragraph");

paragraph.innerHTML = "Hello its a new text!";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM