簡體   English   中英

如何通過 JavaScript 更改元素的文本?

[英]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>

我嘗試過 textContent 和 innerHTML,我嘗試通過 ID、類和標簽獲取元素,但沒有任何效果。 我很確定我錯過了一些東西。 我如何更改標題中的段落以及標題本身?

試試這段代碼,將段落的 id 屬性粘貼到 getElementById 中。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM