开发者

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

开发者 https://www.devze.com 2022-12-19 19:57 出处:网络
So I\'m new to JavaScript and I\'m trying to figure out why doesn\'t thi开发者_开发问答s work:

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

精彩评论

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