Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Javascript .innerHTML deletes everything else inside the website

I am completely new to JS, HTML and CSS, and I am creating a small project using javascript. The project has multiple divs for boxes

My issue is whenever I use document.getElementById('id').innerHTML = 'test' , to change text inside a div, the text in the div does change, but everything else disappears from the website.

Would greatly appreciate any help!


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

1 Answer

0 votes
by (71.8m points)

It seems that your problem lies with the =. You're using = instead of +=.

x = "text" will erase whatever value the variable previously had, since you're redefining the variable x.

x += "text" is equivalent to doing x = x + "text", which is setting x equal to x first, then adding your text.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...