2017-01-06 112 views
1

我知道有很多話題,但沒有人回答我的問題。 我必須打印字典中最小和最大的窗口屬性。但我得到了錯誤Cannot set property of innerHTML of null at solve。 這裏是我的html:無法設置屬性innerHTML空(javascript)

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <title>Loops Homework</title> 
    <meta charset="UTF-8"> 

</head> 

<body> 
    <span>Problem 1</span> 
    <button onclick="solve()">Print</button> 
    <div id="answer1">Answer will appear here</div> 
    <script src="LexicographicallyProp.js" type="text/javascript"></script> 
</body> 

</html> 

而且下面的JavaScript文件:

function solve() { 
let smallest = 'zzz', 
    largest = '', 
    prop, 
    div1 = document.getElementById('#answer1'); 

for (prop in document) { 
    if (prop < smallest) { 
     smallest = prop; 
    } 

    if (prop > largest) { 
     largest = prop; 
    } 
} 
div1.innerHTML = 'Smallest property in DOCUMENT: ' + smallest + ' ; Largest prop in DOCUMENT: ' + largest; 

}

+1

從的document.getElementById( '#ANSWER1')刪除#號; – Gregg

回答

1

它應該是:

div1 = document.getElementById('answer1'); 

'#answer1'是jQuery的符號來表示HTML元素ID 。

1

請更改以下行:

div1 = document.getElementById('#answer1'); 

用途:

div1 = document.getElementById('answer1'); 
相關問題