我的腳本標簽位于我的正文標簽的底部,這是我在這里找到的主要解決方案。是否還有其他原因 appendChild 會遇到此錯誤?
html:
<section class="section-2">
<p class="about-me-text"></p>
</section>
<script src="rScript.js"></script>
</body>
Javascript:
const aboutMeText = document.querySelector("about-me-text");
const aboutMeTextContent = 'I am a creative designer, who dabbles in both website
creation & digital art design. Contact me to start a creative project or website
today.'
Array.from(aboutMeTextContent).forEach(char => {
const span = document.createElement("span");
span.textContent = char;
aboutMeText.appendChild(span);
})
uj5u.com熱心網友回復:
您需要添加一個“。” 定位類名時查詢選擇器()。
const aboutMeText = document.querySelector(".about-me-text");
const aboutMeTextContent = `I am a creative designer, who dabbles in both website
creation & digital art design. Contact me to start a creative project or website
today.`
Array.from(aboutMeTextContent).forEach(char => {
const span = document.createElement("span");
span.textContent = char;
aboutMeText.appendChild(span);
})
<section class="section-2">
<p class="about-me-text"></p>
</section>
uj5u.com熱心網友回復:
使用時querySelector,您必須明確您選擇的是哪個元素,例如,在您的問題中,當您選擇一個類時,您必須.在about-me-text. 對于 ID,您將#在開頭添加一個等。
const aboutMeText = document.querySelector(".about-me-text");
const aboutMeTextContent = 'I am a creative designer, who dabbles in both website creation & digital art design. Contact me to start a creative project or website today.'
Array.from(aboutMeTextContent).forEach(char => {
const span = document.createElement("span");
span.textContent = char;
aboutMeText.appendChild(span);
})
<section class="section-2">
<p class="about-me-text"></p>
</section>
<script src="rScript.js"></script>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/452208.html
標籤:javascript html 数组 dom 附子
上一篇:將JavascriptDOM函式轉換為REACTJS
下一篇:洗掉JavaScript中的點擊
