我正在構建迷你搜索引擎,因爲我正在學習JavaScript和React的基礎知識。我使用prompt()構建了函數引擎,然後使用for循環來查找匹配,然後根據某些狀態的屬性返回不同的結果。瀏覽器無法識別使用React的JSX
的index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Venos</title>
<script src="https://unpkg.com/[email protected]/dist/react.js"></script>
<script src="https://unpkg.com/[email protected]/dist/react-dom.js"></script>
<script src="index.js"></script>
</head>
<body>
<div id="app">
<div id="react-app">
</div>
</div>
</body>
</html>
index.js:發現
var searchInput = prompt('Search: ');
var states = {
'North Dakota': {capital: {name: 'Bismarck', namedAfter: 'Ferdinand Bismarck'}, region: 'Mid-west'},
Minnesota: {capital: 'Saint paul', region: 'Mid-west'},
Montana: {capital: 'Helena', region: 'Mid-west'},
Wisconsin: {capital: 'Madison', region: 'Mid-west'}
};
var searchCapitals = function(givenWord){
for (var key in states) {
if (givenWord.toLowerCase() === key.toLowerCase()) {
var found = true
var foundKey = key
break;
}
}
if (found == true){
if (states[foundKey].capital.name){
var foundSearchComplex = (
<div>
// html built from {'Search found: ' + foundKey + ' (capital: ' + states[foundKey].capital.name + ' (named after ' + states[foundKey].capital.namedAfter + '), region: ' + states[foundKey].region + ')'}
<h4>Search Found</h4>
<ul>
<li>Capital: {states[foundKey].capital.name}</li>
<li>Capital named after: {states[foundKey].capital.namedAfter}</li>
<li>Region: {states[foundKey].region}</li>
</ul>
</div>
)
ReactDOM.render(foundSearchComplex, document.getElementById('react-app'));
} else
var foundSearchSimple = (
// html built from {'Search found: ' + foundKey + ' (capital: ' + states[foundKey].capital.name + , region: ' + states[foundKey].region + ')'}
<div>
<h4>Search Found</h4>
<ul>
<li>Capital: {states[foundKey].capital.name}</li>
<li>Region: {states[foundKey].region}</li>
</ul>
</div>
};
ReactDOM.render(foundSearchSimple, document.getElementById('react-app'));
} else {
console.log('No results found.')
}
)
searchCapitals(searchInput);
錯誤:
index.js:21 Uncaught SyntaxError: Unexpected token <
我明白,我寫清楚了什麼。我只是不明白:(
所以我可以問,爲什麼當你聲明foundSearchComplex和foundSearchSimple時使用'{}'?既然你嘗試渲染它們,你應該使用'()',而不是當你聲明它們時 –
我修正了。錯誤仍然存在。 –
你需要一個譯員來編譯代碼 – Li357