How can I use innerhtml in JavaScript?

前端 未结 4 2061
无人及你
无人及你 2020-12-17 17:37

My problem is that I don\'t know how to show the innerhtml of my form.

The form is like a survey form and once you clicked the submit button, all the contents I had

相关标签:
4条回答
  • 2020-12-17 18:17

    What is your element with id "contend" , a div? td? tr? or ? The way it should be is

     <div id='content'> 
       </div>
    

    and then this is the javascript code

    document.getElementById("content").innerHTML = "<p>" + first + middle + last + "</p>"
    
    0 讨论(0)
  • 2020-12-17 18:24

    Try this:

    But , you should have id as first. and you should need content div in html part.

    <script>
    function displayResult() {
        var first = document.getElementById("first").value;
    
        var maincontent = "";
        maincontent = "<p>" + first + "</p>";
        document.getElementById("content").innerHTML = maincontent;
    
    }
    </script>
    <body>
    <input type="text" id="first" value="good">
    <button onclick="displayResult();">Click me!!!</button>
    <div id="content"></div>
    </body>
    
    0 讨论(0)
  • 2020-12-17 18:34
    var maincontent = document.getElementById("content").innerHTML;
    maincontent = "<p>" + first;
    

    On the second line you're overwriting the variable, not setting the .innerHTML. This is what you want:

    var maincontent = document.getElementById("content");
    maincontent.innerHTML = "<p>" + first;
    

    Also, you must make sure the elements with ids of "first" "middle" and "last" actually exist, or this might cause a TypeError.

    0 讨论(0)
  • 2020-12-17 18:41

    Instead of this:

    var maincontent = document.getElementById("content").innerHTML;
    maincontent = "<p>" + first;`
    

    Try this:

    document.getElementById("content").innerHTML = "<p>" + first;
    

    You may also want to use .innerHTML to get 'first', rather than .value? I'm not sure what kind of element 'first' is.

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