【问题标题】:Why is my output repeating?为什么我的输出重复?
【发布时间】:2016-09-20 23:12:14
【问题描述】:

我想提醒“正在运行的大炮”,然后当我按下确定时,我只想说“大炮船航行到 14 度”,但它会继续在我的输出中打印警报。

JS

function alertMessage (message) {
alert (message);
}
alertMessage("the Battle has begun");



function alertShip (ship, number) {
alert (ship);
document.write (ship + "the ship sails off to " + number + "degrees");

}
alertShip("running function cannons", 14);

HTML

<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8">
  <title>Functions</title>

    <!-- This links to the js code specific for this page -->
    <script src="functions.js"></script>
</head>
<body>



  <div id="output"> </div>  
 <div id="output2"> </div>  

</body>
</html>

【问题讨论】:

  • 不用document.write试试

标签: javascript function parameters output alert


【解决方案1】:

尝试从 document.write 中删除 ship 参数并将语句替换为:

document.write("炮舰航行到" + number + "度");

然后,你应该能够得到你想要的输出。

*p.s.:如果您在文本编辑器上执行此操作并尝试在浏览器上运行此操作,您可以考虑将标记更改为:.

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    不要给他们相同的变量来输出.. alert (ship) 然后 document.write (ship+.....)。 试试这个

    function alertShip (ship, number) {
    alert (ship);
    document.write ("Cannon ship sails off to " + number + "degrees");
    

    【讨论】:

    • 谢谢。就是这样。
    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    相关资源
    最近更新 更多