我正在嘗試使用輸入提交類型字段進行搜索。一旦用戶輸入內容,它應該查看div中的值。如果它匹配其中的一個,那麼它應該突出顯示黃色的div背景,如果它沒有在div列表的底部添加新值。突出顯示匹配輸入值輸入的div javascript
我能凸顯的背景它匹配的時候,但只突出停留一秒鐘,disappears.Also,它不列表中的「機器學習」的第二個元素相匹配。對於添加列表底部的第二部分,我嘗試推送列表中的新值,但這也不起作用。
有什麼建議嗎?
HTML和JS: `
function searchList() {
var searchCourse = document.getElementById("search").value;
\t var courseList = document.getElementById("courselist").getElementsByTagName("DIV");
\t for(var i=0; i<courseList.length; i++) {
\t \t var course = courseList[i];
\t \t var coursecheck = course.innerHTML;
\t \t
\t \t if(searchCourse == coursecheck){
\t \t \t course.style.backgroundColor = 'yellow';
\t \t }
\t }
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
\t <head><title> SOEN 287 NEW Exercise </title>
\t \t <style type="text/css">
\t \t \t fieldset {border:0px;}
\t \t \t #courselist {width:300px;}
\t \t \t #courselist div {border: 1px black solid;padding:10px;}
\t \t </style>
\t </head>
\t <body>
\t \t <div id="container">
\t \t \t <h2>Search a Course</h2>
\t \t \t <form action="" method="post" onsubmit="return searchList()">
\t \t \t \t <fieldset>
\t \t \t \t \t Enter the Course Name<br />
\t \t \t \t \t <input type="text" id="search" size="20" /><br />
\t \t \t \t \t <input type="submit" value="Search List" id="sub" />
\t \t \t \t \t <br /><br />
\t \t \t \t </fieldset>
\t \t \t </form>
\t \t \t <div id="courselist">
\t \t \t \t <div id="first"> </div>
\t \t \t \t <div> Machine Learning </div>
\t \t \t \t <div> Image Processing</div>
\t \t \t \t <div>Design and Analysis of Algorithms</div>
\t \t \t \t <div>Web Programming II </div>
\t \t \t \t <div>Advanced JAVA</div>
\t \t \t \t <div>Pattern Recognition</div>
\t \t \t </div>
\t \t </div>
\t \t <script type="text/javascript" src="main.js"></script>
\t </body>
</html>
*「......但亮點僅停留一秒鐘消失」 * - 這是因爲提交按鈕被請求頁面刷新。你會希望通過在函數中執行'return false'或'event.preventDefault()'來防止默認行爲。 –