How do I add something to my html using javascript without erasing the whole page

前端 未结 3 1742
北恋
北恋 2020-12-02 02:45

I have a small calculator that I\'m adding to my html. It has a few dropdowns to select stuff and when a person hits a submit button, I\'d like to display a picture below th

相关标签:
3条回答
  • 2020-12-02 03:15

    Instead of document.write() you can set the innerHTML property of any DOM node to add text to the page.

    Say you add a <div id='result'></div> to the end of the page (before the body tag). Then in your javascript have:

    var result_display = document.getElementById('result');
    // and in calc_rate():
    result_display.innerHTML = "answer";
    

    Note that this will always reset the result_display's text. You can also wrap that operation in a function:

    function displayResult(result){
        result_display.innerHTML = '<h2>' + result + '</h2>'; // or whatever formatting
    }
    
    0 讨论(0)
  • 2020-12-02 03:18

    You need a placeholder element for your output. Then set the innerHTML for that element:

    <div id='answer'></div>
    

    then:

    document.getElementById('answer').innerHTML = "answer is:" + yourdata
    
    0 讨论(0)
  • 2020-12-02 03:21

    Don't use document.write, period. Use DOM operations: http://www.quirksmode.org/dom/intro.html

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