简体   繁体   中英

Add HTML code in .html files with javascript

I'm developing a website with bootstrap. If I want to modify the navbar, I don't want to go to any html files and make changes. So would like to use javascript to "inject" the html code into the actual html file but I don't know how to do it.

This is what I tried.

document.write("<p>html code here</p>")

It, however, doesn't work. What the most conveniente and simple solution could be?

You can try something like this example below:

let newElement = document.createElement('div');

newElement.className = 'new-element';

This will create a div - assign it to the variable named 'newElement' and creating a class associated with that element named "new-element".

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