The problem of JavaScript position
-
Dear All, I got a question! why can't i put the script code after the div with class="jumbotorn" ? I have tried to put it in the , and it's work! Please helps me! THANKS!
<body>
<div class="jumbotron">
<div align="center">
<h1 style="font-family:微軟正黑體;">行銷神語</h1>
<p id="pharse"></p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
</div><script> function makePharse(){ var word1 = \["24/7","Multi-Tier","30,000 foot","B-to-B","Win-Win","Customer"\]; var word2 = \["Empowered","Value-Added","Oriented","Focused","Aligned"\]; var word3 = \["Process","Solution","Tipping-Point","Strategy","Vision"\]; var rand1 = Math.floor(Math.random()\*word1.length); var rand2 = Math.floor(Math.random()\*word2.length); var pharse = word1\[rand1\]+""+word2\[rand2\]+""; var pharseElement = document.getElementById("pharse"); pharseElement.innerHTML = pharse; pharseElement.innerHTML = "TESt"; } </script>
</body>
-
Dear All, I got a question! why can't i put the script code after the div with class="jumbotorn" ? I have tried to put it in the , and it's work! Please helps me! THANKS!
<body>
<div class="jumbotron">
<div align="center">
<h1 style="font-family:微軟正黑體;">行銷神語</h1>
<p id="pharse"></p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
</div><script> function makePharse(){ var word1 = \["24/7","Multi-Tier","30,000 foot","B-to-B","Win-Win","Customer"\]; var word2 = \["Empowered","Value-Added","Oriented","Focused","Aligned"\]; var word3 = \["Process","Solution","Tipping-Point","Strategy","Vision"\]; var rand1 = Math.floor(Math.random()\*word1.length); var rand2 = Math.floor(Math.random()\*word2.length); var pharse = word1\[rand1\]+""+word2\[rand2\]+""; var pharseElement = document.getElementById("pharse"); pharseElement.innerHTML = pharse; pharseElement.innerHTML = "TESt"; } </script>
</body>
I do not agree that your script will work inside the head. makePharse() is a function that you have to call it on some event say button click, so add an onclick to your button:
<a class="btn btn-primary btn-lg" href="#" role="button" onclick="makePharse()">Learn more</a>
and comment out the
//pharseElement.innerHTML = "TESt";
-
Dear All, I got a question! why can't i put the script code after the div with class="jumbotorn" ? I have tried to put it in the , and it's work! Please helps me! THANKS!
<body>
<div class="jumbotron">
<div align="center">
<h1 style="font-family:微軟正黑體;">行銷神語</h1>
<p id="pharse"></p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more</a></p>
</div>
</div><script> function makePharse(){ var word1 = \["24/7","Multi-Tier","30,000 foot","B-to-B","Win-Win","Customer"\]; var word2 = \["Empowered","Value-Added","Oriented","Focused","Aligned"\]; var word3 = \["Process","Solution","Tipping-Point","Strategy","Vision"\]; var rand1 = Math.floor(Math.random()\*word1.length); var rand2 = Math.floor(Math.random()\*word2.length); var pharse = word1\[rand1\]+""+word2\[rand2\]+""; var pharseElement = document.getElementById("pharse"); pharseElement.innerHTML = pharse; pharseElement.innerHTML = "TESt"; } </script>
</body>