嗨,当输入与我列表中的一个值匹配时,我正在尝试更改div标记的背景样式颜色,如果不匹配,我想创建一个div标记,并将缺失的值附加到列表底部,因为它不匹配。
我四处搜索,找到了这个线程,并使用了相同的方法,但没有运气。以下是我对外部js脚本的尝试:
function searchList()
{
var input = document.getElementById("search").value;
if ((input == "")||(input == null))
{
alert ('Error', 'values missing');
}
var childDivs = document.getElementById('courselist').getElementsByTagName('div');
for (i = 0; i < childDivs.length; i++)
{
var childDiv = childDivs[i];
if (input = childDiv)
{
document.getElementById("container").style.backgroundColor = yellow;
document.getElementById("courselist").style.backgroundColor = yellow;
}
else if (input != childDiv)
{
var div = document.createElement("div");
div.innerHTML = input;
document.courselist.appendChild(div);
}
}
}
<?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">
<head><title> Work</title>
<style type="text/css">
fieldset {border:0px;}
#courselist {width:300px;}
#courselist div {border: 1px black solid;padding:10px;}
</style>
</head>
<body>
<div id="container">
<h2>Search a Course</h2>
<form action="" method="post" onsubmit="return searchList()">
<fieldset>
Enter the Course Name<br />
<input type="text" id="search" size="20" /><br />
<input type="submit" value="Search List" id="sub" />
<br /><br />
</fieldset>
</form>
<div id="courselist">
<div id="first"> </div>
<div> Machine Learning </div>
<div> Image Processing</div>
<div>Design and Analysis of Algorithms</div>
<div>Web Programming II </div>
<div>Advanced JAVA</div>
<div>Pattern Recognition</div>
</div>
</div>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
用函数更改div的样式并用javascript附加新div的正确方法是什么?提前感谢!
黄色应该是一个字符串,您需要在页面中找到正确的元素
function searchList()
{
var input = document.getElementById("search").value;
if ((input == "")||(input == null))
{
alert ('Error', 'values missing');
}
var childDivs = document.getElementById('courselist').getElementsByTagName('div');
for (i = 0; i < childDivs.length; i++)
{
var childDiv = childDivs[i];
if (input === childDiv)
{ if(input === 'Machine Learning'){
document.getElementById("#courselist").find('.machineLearning').style.backgroundColor = 'yellow';
}
}
else if (input != childDiv)
{
var div = document.createElement("div");
div.innerHTML = input;
document.courselist.appendChild(div);
}
}
}
<?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">
<head><title> Work</title>
<style type="text/css">
fieldset {border:0px;}
#courselist {width:300px;}
#courselist div {border: 1px black solid;padding:10px;}
</style>
</head>
<body>
<div id="container">
<h2>Search a Course</h2>
<form action="" method="post" onsubmit="return searchList()">
<fieldset>
Enter the Course Name<br />
<input type="text" id="search" size="20" /><br />
<input type="submit" value="Search List" id="sub" />
<br /><br />
</fieldset>
</form>
<div id="courselist">
<div id="first"> </div>
<div class = 'machineLearning'> Machine Learning </div>
<div> Image Processing</div>
<div>Design and Analysis of Algorithms</div>
<div>Web Programming II </div>
<div>Advanced JAVA</div>
<div>Pattern Recognition</div>
</div>
</div>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
- 将
yellow
作为字符串传递,因为没有包含value
的yellow
变量 - 测试
Element.textContent
属性,而不是元素本身,否则结果将始终为false
- 如果找到值,则中断循环
- 保留一个变量以测试找到的值,否则追加元素
- 使用
document.getElementById('courselist')
而不是document.courselist
访问元素 - 在函数中使用
return false;
阻止表单提交
function searchList() {
var input = document.getElementById("search").value;
if ((input == "") || (input == null)) {
return alert('Error', 'values missing');
}
var childDivs = document.getElementById('courselist').getElementsByTagName('div');
var found = false;
for (i = 0; i < childDivs.length; i++) {
var childDiv = childDivs[i];
if (input == childDiv.textContent) {
document.getElementById("container").style.backgroundColor = 'yellow';
document.getElementById("courselist").style.backgroundColor = 'yellow';
found = true;
break;
}
}
if (!found) {
document.getElementById("container").style.backgroundColor = '';
document.getElementById("courselist").style.backgroundColor = '';
//If you want to remove the `backgroundColor`
var div = document.createElement("div");
div.innerHTML = input;
document.getElementById('courselist').appendChild(div);
}
return false;
}
fieldset {
border: 0px;
}
#courselist {
width: 300px;
}
#courselist div {
border: 1px black solid;
padding: 10px;
}
<div id="container">
<h2>Search a Course</h2>
<form action="" method="post" onsubmit="return searchList()">
<fieldset>
Enter the Course Name
<br />
<input type="text" id="search" size="20" />
<br />
<input type="submit" value="Search List" id="sub" />
<br />
<br />
</fieldset>
</form>
<div id="courselist">
<div id="first"> </div>
<div>Machine Learning</div>
<div>Image Processing</div>
<div>Design and Analysis of Algorithms</div>
<div>Web Programming II</div>
<div>Advanced JAVA</div>
<div>Pattern Recognition</div>
</div>
</div>