App下載
首頁javascriptparagraphJavascript Element - 如何為段落標(biāo)簽分配隨機(jī)名稱

Javascript Element - 如何為段落標(biāo)簽分配隨機(jī)名稱

我們想知道如何為段落標(biāo)簽分配隨機(jī)名稱。

<!DOCTYPE html>
<html>
<head>
<title>Random Name</title>
</head>
<body>
  <div id="container">
    <p id="name"></p>
  </div>
  <script>
var randomName = function() {
  var arry = ["Kevin", "John", "Mabel", "Lucy", "Isabella", "Ryan", "Clyde"];
    var random = arry[Math.round(Math.random() * arry.length)];
    return random;
};
// create button
var button = document.createElement("input");
var id = "btn";
button.type = "submit";
button.value = "click me!";
button.setAttribute("id", "btn");
document.body.appendChild(button);

window.onload = function() {
  document.getElementById("btn").addEventListener("click", function(e) {
    document.getElementById("name").innerHTML = randomName();
     console.log("hi");
    }   
  );
};
</script>
</body>
</html>