Document.write function replaces the whole page with the text

前端 未结 2 1846
無奈伤痛
無奈伤痛 2020-12-12 04:28

I am relatively new to javascript and I created a function to multiply two numbers together, but document.write replaces the whole page with the answer. I know

相关标签:
2条回答
  • 2020-12-12 05:29

    If you add an empty element like <span id="fillme"></span> to the document you can use this with var fillme = document.getElementById("fillme"); and then later assign content to fillme with fillme.innerHTML = answer;

    0 讨论(0)
  • 2020-12-12 05:31

    You already know how to use document.getElementById() to retrieve the values of inputs on your page. So do something similar to update an element that holds the answer:

    <div id="answer"></div>
    

    ...and then:

    document.getElementById("answer").innerHTML = answer;
    

    Simle demo: http://jsfiddle.net/V5R5h/1/

    0 讨论(0)
提交回复
热议问题