开发者

Why can't I set value to display in a div?

So I'm new to JavaScript and I'm trying to figure out why doesn't thi开发者_开发问答s work:

My function has this line:

document.getElementById("displayResult").value = ("test");

and this is my div:

<div id="displayResult"></div>


div's don't have a value property. You want to set the .innerText property.

And by all means, have fun testing things yourself, but you'll find it a lot easier if you use a framework to do these things (like jQuery)


You will want to use - .innerHTML no?

document.getElementById("displayResult").innerHTML = "<b>test</b>"; 


.value is only a valid attribute on form fields. You likely want to use the following code:

document.getElementById("displayResult").innerHTML = "test";


you have to test if innerHTML is supported by your brwser. As it is not the DOM Standard. You can write it like

var oDiv = document.getElementById("displayResult")

if(typeof oDiv.innerHTML != undefined) {
      oDiv .innerHTML = message;
    } else {
      oDiv .appendChild(document.createTextNode(message));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜