Javascript:如何创建新div并更改背景颜色

时间:2016-07-30 19:58:19

标签: javascript html styles append external

您好我正在尝试更改div标签的背景样式颜色,当输入与我的列表中的某个值匹配时,如果它没有,我想创建div标签并追加列表底部的缺失值,因为它不匹配。

我四处搜索并发现this线程并使用了相同的方法,但没有运气。这是我对外部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);
    }
  }
}
&#13;
<?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">&nbsp;</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>
&#13;
&#13;
&#13;

使用函数更改div的样式以及使用javascript附加新div的正确方法是什么?提前谢谢!

2 个答案:

答案 0 :(得分:2)

黄色应该是一个字符串,你需要找到带有页面的正确元素

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">&nbsp;</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>

答案 1 :(得分:1)

  • yellow作为字符串传递,因为没有yellow变量持有value
  • 测试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">&nbsp;</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>