简体   繁体   中英

Div's css style now showing up when javascript is inside of it

I have a pretty simple page (for practicing right now). I have a div with a red border and a blue background. Inside the div is a tag calling an external js file. The js is working, but then the border of the div disappears.

Here is the code.

 var myArray = [ "Item one", "Item two", "Item three" ]; var randomItemOne = myArray[Math.floor(Math.random() * myArray.length)]; document.body.innerHTML = randomItemOne; 
 .box1 { border: solid 2px red; width: 200px; height: 150px; background-color: skyblue; } 
 HTML <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Random Text Selector</title> <meta name="description" content="The HTML5 Herald"> <link rel="stylesheet" href="rt_style.css"> </head> <body> <div class="box1"> <script src="js/random.js"></script> </div> </body> </html> 

 var myArray = [ "Item one", "Item two", "Item three" ]; var randomItemOne = myArray[Math.floor(Math.random() * myArray.length)]; document.getElementsByClassName("box1")[0].innerText = randomItemOne; 
 .box1 { border: solid 2px red; width: 200px; height: 150px; background-color: skyblue; } 
 HTML <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Random Text Selector</title> <meta name="description" content="The HTML5 Herald"> <link rel="stylesheet" href="rt_style.css"> </head> <body> <div class="box1"> <script src="js/random.js"></script> </div> </body> </html> 

I think this is what you are trying to achieve. Please correct me if I'm wrong

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