2015-02-07 40 views
0

我寫了正常運行控制檯一個簡單的代碼,但不反映在代碼中對象的屬性,我不能做出來什麼是錯的代碼:對象不反映在段落屬性

<html> 
<head> 
<script> 
function alpha(){ 
var x = {name:"Sunil",age:37,gender:"male"}; 
document.getElementById('para1').innerHTML(x.name); 
console.log(x.name); 
} 
</script> 
</head> 
<body> 
<button id=but1 onclick=alpha()>Click Me</button> 
<p id=para1> This is paragraph One. </p> 
</body> 
</html> 

請,表示我錯在哪裏,因爲如果我從頭部刪除#para1行,控制檯工作正常,代碼相同!

在此先感謝!

+0

只需添加一些@ Guffa的答案,你會看到這種類型的東西有像jQuery庫其中'的.html(「你好」)'是做正確的事,因爲'html'方法允許你設置和獲取值。 – Andy 2015-02-07 16:52:08

回答

1

innerHTML不是一個方法,它是一個屬性。文本分配給屬性:

document.getElementById('para1').innerHTML = x.name; 
+0

我可以問你一些事嗎? – innovation 2015-02-07 16:48:02

+0

@innovation:你剛剛做到了。 ;) – Guffa 2015-02-07 16:48:31

+0

你能檢查我的問題嗎? [CLICK](http://stackoverflow.com/questions/28377882/tumblr-style-userhovercard) – innovation 2015-02-07 16:49:50