当javascript在其中时,Div的css样式现在出现了

时间:2018-06-08 04:41:40

标签: javascript css

我有一个非常简单的页面(现在就练习)。我有一个红色边框和蓝色背景的div。 div内部是一个调用外部js文件的标签。 js正在工作,但是div的边界消失了。

这是代码。

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>

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
var myArray = [
  "Item one",
  "Item two",
  "Item three"
];

var randomItemOne = myArray[Math.floor(Math.random() * myArray.length)];

document.getElementsByClassName("box1")[0].innerText = randomItemOne;
&#13;
.box1 {
  border: solid 2px red;
  width: 200px;
  height: 150px;
  background-color: skyblue;
}
&#13;
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>
&#13;
&#13;
&#13;

我认为这是你想要实现的目标。如果我错了,请纠正我