How can I use innerhtml in JavaScript?

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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 answered would show like a summary page...

function displayResult() {     var first = document.getElementById("first").value;      var middle = document.getElementById("middle").value;      var last = document.getElementById("last").value;      alert("oh");     var maincontent = document.getElementById("content").innerHTML;     maincontent = "

" + first; }

回答1:

var maincontent = document.getElementById("content").innerHTML; maincontent = "

" + 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 = "

" + first;

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



回答2:

Try this:

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

 


回答3:

Instead of this:

var maincontent = document.getElementById("content").innerHTML; maincontent = "

" + first;`

Try this:

document.getElementById("content").innerHTML = "

" + first;

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



回答4:

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

 

and then this is the javascript code

document.getElementById("content").innerHTML = "

" + first + middle + last + "

"


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!