我是编程新手,没有 JavaScript 经验,我有一项任务要为我的学校做。我需要创建一个单词搜索游戏,它有一个表单/按钮搜索,可以找到一个单词并在网格中突出显示它。我已经创建了 HTML/CSS 布局,但我坚持将其链接到 JavaScript。我希望能够搜索“曼谷”、“伦敦”、“新加坡”、“哈瓦那”和“京都”等词,但我只能搜索/突出显示网格的一个字母/单元格,而我没有'不知道如何从网格中的字母中制作一个字符串,以便我可以找到这些单词。我被困在这里。真的很想明白。有人可以帮我/指导/向我解释它是如何工作的吗?我已经阅读了很多东西,但似乎找不到我要找的东西。我真的很感激。
这是我到目前为止所拥有的:https ://jsfiddle.net/fwg8hequ/10/
function search() {
var text = document.getElementById("query").value;
var query = new RegExp("(\\b" + text + "\\b)", "gim");
var e = document.getElementById("searchtext").innerHTML;
var enew = e.replace(/(<span>|<\/span>)/igm, "");
document.getElementById("searchtext").innerHTML = enew;
var newe = enew.replace(query, "<span>$1</span>");
document.getElementById("searchtext").innerHTML = newe;
}
@charset "UTF-8";
/* CSS Document */
@font-face {
font-family: 'RobotoSlab';
src: url('RobotoSlab-bold.ttf');
}
@font-face {
font-family: 'RobotoMono';
src: url('RobotoMono-Regular.ttf');
}
.container {
position: relative;
width: 1000px;
height: 800px;
background: #ffcc78;
}
.header {
position: absolute;
left: 24.7%;
right: 26%;
top: 5.25%;
bottom: 86.75%;
overflow: auto;
}
.header img {
width: 58px;
height: 58px;
left: 247px;
top: 46px;
float: left;
}
.header h1 {
left: 33.8%;
right: 28.4%;
width: 378px;
height: 64px;
font-family: RobotoSlab;
font-style: normal;
line-height: normal;
font-size: 48px;
letter-spacing: -1px;
color: #E25C5C;
line-height: 5.28%;
float: right;
}
form {
position: absolute;
left: 24.7%;
right: 26%;
top: 18.75%;
bottom: 75%;
}
input[type=text] {
float: left;
left: 24.7%;
right: 35.8%;
top: 18.75%;
bottom: 75%;
width: 410px;
height: 50px;
background: #FFFFFF;
border: 1px solid #417505;
box-sizing: border-box;
border-radius: 5px;
}
button {
position: absolute;
left: 66.5%;
right: 27.3%;
top: 18.75%;
bottom: 75%;
background: linear-gradient(180deg, #76AD0C 0%, #417505 100%);
border-radius: 5px;
font-family: RobotoSlab;
font-style: normal;
line-height: normal;
font-size: 15px;
color: #FFFFFF;
float: right;
}
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto auto auto auto auto auto auto;
background-color: #E25C5C;
position: absolute;
left: 24.7%;
right: 26%;
top: 30.5%;
bottom: 7.88%;
padding: 2px;
border-radius: 5px;
}
.grid-item {
background-color: #ffcc78;
border: 2px solid #E25C5C;
left: 26.2%;
right: 27.2%;
font-family: RobotoMono;
line-height: 36px;
font-size: 36px;
letter-spacing: 2.9px;
font-style: normal;
font-weight: normal;
text-align: center;
padding: 2px;
}
#searchtext span {
background-color: #F5A623;
}
<div class="container">
<div class="header">
<img src="icon.png" alt="Icon" height="58" width="58">
<h1>WORD SEARCH</h1>
</div>
<form>
<input name="query" id="query" type="text">
</form>
<button type="button" onClick="search();">SEARCH</button>
<div class="grid-container" id="searchtext">
<div class="grid-item">W</div>
<div class="grid-item">S</div>
<div class="grid-item">I</div>
<div class="grid-item">A</div>
<div class="grid-item">L</div>
<div class="grid-item">C</div>
<div class="grid-item">E</div>
<div class="grid-item">O</div>
<div class="grid-item">I</div>
<div class="grid-item">V</div>
<div class="grid-item">V</div>
<div class="grid-item">A</div>
<div class="grid-item">L</div>
<div class="grid-item">B</div>
<div class="grid-item">A</div>
<div class="grid-item">N</div>
<div class="grid-item">G</div>
<div class="grid-item">K</div>
<div class="grid-item">O</div>
<div class="grid-item">K</div>
<div class="grid-item">U</div>
<div class="grid-item">T</div>
<div class="grid-item">L</div>
<div class="grid-item">O</div>
<div class="grid-item">N</div>
<div class="grid-item">D</div>
<div class="grid-item">O</div>
<div class="grid-item">N</div>
<div class="grid-item">O</div>
<div class="grid-item">I</div>
<div class="grid-item">U</div>
<div class="grid-item">S</div>
<div class="grid-item">I</div>
<div class="grid-item">N</div>
<div class="grid-item">G</div>
<div class="grid-item">A</div>
<div class="grid-item">P</div>
<div class="grid-item">O</div>
<div class="grid-item">R</div>
<div class="grid-item">E</div>
<div class="grid-item">A</div>
<div class="grid-item">L</div>
<div class="grid-item">C</div>
<div class="grid-item">O</div>
<div class="grid-item">G</div>
<div class="grid-item">E</div>
<div class="grid-item">E</div>
<div class="grid-item">U</div>
<div class="grid-item">V</div>
<div class="grid-item">R</div>
<div class="grid-item">H</div>
<div class="grid-item">A</div>
<div class="grid-item">V</div>
<div class="grid-item">A</div>
<div class="grid-item">N</div>
<div class="grid-item">A</div>
<div class="grid-item">T</div>
<div class="grid-item">L</div>
<div class="grid-item">A</div>
<div class="grid-item">A</div>
<div class="grid-item">A</div>
<div class="grid-item">B</div>
<div class="grid-item">I</div>
<div class="grid-item">S</div>
<div class="grid-item">S</div>
<div class="grid-item">N</div>
<div class="grid-item">O</div>
<div class="grid-item">R</div>
<div class="grid-item">I</div>
<div class="grid-item">S</div>
<div class="grid-item">N</div>
<div class="grid-item">K</div>
<div class="grid-item">Y</div>
<div class="grid-item">O</div>
<div class="grid-item">T</div>
<div class="grid-item">O</div>
<div class="grid-item">A</div>
<div class="grid-item">H</div>
<div class="grid-item">B</div>
<div class="grid-item">E</div>
<div class="grid-item">Z</div>
<div class="grid-item">M</div>
<div class="grid-item">P</div>
<div class="grid-item">T</div>
<div class="grid-item">R</div>
<div class="grid-item">E</div>
<div class="grid-item">S</div>
<div class="grid-item">J</div>
<div class="grid-item">R</div>
<div class="grid-item">L</div>
<div class="grid-item">F</div>
<div class="grid-item">P</div>
<div class="grid-item">E</div>
<div class="grid-item">K</div>
<div class="grid-item">T</div>
<div class="grid-item">A</div>
<div class="grid-item">M</div>
<div class="grid-item">L</div>
<div class="grid-item">O</div>
<div class="grid-item">J</div>
</div>
</div>