更新:将Li标记添加到每个数组项

时间:2015-06-21 23:37:27

标签: javascript

所以在你所有的帮助之后我已经达到了向内容区域添加输入的阶段,现在我想在用户点击右边的红色十字按钮时删除列表项,我该如何定位我添加的类标签'点击链接'从功能外部?

由于



(function(){

    var clickGo = document.querySelector('.button-go');
    var searchInput = document.querySelector('.search-input');
    var list = document.querySelector('.list');
    var listArray = [];

    var clickGoFunc = function() {
            var searchValue = searchInput.value;

            // check if input field is empty
            if(searchValue === ""){
                
                alert("please put a value in the input field");
            
            }else{

                // check if list item already exists in the array named listArray
                if(listArray.indexOf(searchValue) == -1){
                    listArray.push(searchValue);

                        // Function for creating loop for added array items and enclosing each array item inside <li>
                        //Also adding a close button to each array item by adding <a> Tag
                        var runIt = function (){
                                for(var i= 0; i < listArray.length; i++ ){
                                    var liTag = document.createElement('li');
                                    var removeItem = document.createElement('span');
                                    var closeLink = document.createElement('a');
                                    closeLink.className = "close-link";
                                    closeLink.setAttribute('href', "#");
                                    closeLink.textContent = 'X';

                                    // adding <a> tag inside span
                                    removeItem.appendChild(closeLink);

                                    liTag.textContent = listArray[i].toUpperCase();
                                    list.appendChild(liTag);
                                    liTag.appendChild(removeItem);
                                    list.appendChild(liTag)
                                } 
                                // end of for loop
                        }; 
                        // end of runit

                        // clearing ul.list container before reloading array items.
                        list.innerHTML = "";

                        // calling runIt funtion which contains for loop  for all the array items
                        list.appendChild(runIt());

                }else{
                    alert("value already exisits");
                } 
                // end of if and else statement which is checking if value is already there is the array
                // pushing new values in the listArray array 
                // wrapping new array items in li tags and adding close button on each array item
            }
            // if statement checking if the input field is empty , if not it runs other function which checks following 
                    // end of if and else statement which is checking if value is already there is the array
                    // pushing new values in the listArray array 
                    // wrapping new array items in li tags and adding close button on each array item
            } 
            // end of clickGoFunc


    //calling function 
    clickGo.addEventListener('click', function(){
            clickGoFunc()
    });

}());
&#13;
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}table{border-collapse:collapse;border-spacing:0}.container-header{background-color:#00628B}.row-header{min-height:120px;text-align:center;color:#fff;font-size:50px;font-family:oswald;padding-top:35px}.container-search-bar{background-color:#E6E6DC;min-height:75px}.container-search-inner .input-group{margin-top:20px}.container-content{padding-top:50px}.list li{background-color:#6C6C6C;padding:10px;margin-bottom:10px;color:#fff;-moz-border-radius:5px;-webkit-border-radius:5px;border-radius:5px}.close-link{color:#fff;background-color:red;padding:3px;border-radius:3px}.list li span{float:right}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Search NOMO</title></title>
	  <link rel="stylesheet" href="assets/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/stylesheets/css/style.min.css">
</head>
<body>
   <div class="container-fluid container-wrapper">
      <div class="row row-wrapper">
         <div class="container-fluid container-header">
              <div class="row row-header">
                Search NOMO
              </div> <!-- row-header -->
         </div> <!-- container-header -->
         <div class="container-fluid container-search-bar">
             <div class="row row-search">
                <div class="container container-search-inner">
                  <div class="row">
                    <div class="col-lg-12">
                        <div class="input-group">
                          <input type="text" class="form-control search-input" placeholder="Add to List...">
                          <span class="input-group-btn">
                            <button class="btn btn-default button-go" type="button">Add</button>
                          </span>
                        </div><!-- /input-group -->
                      </div><!-- /.col-lg-6 -->
                 </div> <!-- row -->
               </div> <!-- container-search-inner -->
            </div> <!-- row-search -->
        </div> <!-- container-search-bar -->
        <div class="container container-content">
            <ul class="list">
                
            </ul>

        </div>
      </div> <!--row-wrapper -->
   </div> <!-- container-wrapper-->

   <!-- javascripts -->
   <script src="assets/jquery/dist/jquery.min.js"></script>
<script src="assets/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="assets/js/script.min.js"></script>

</body>
</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

您必须将li - 元素附加到某个ul元素。您只需要数组来检查现有值。

有了这些知识,您的代码可以简化为(参见代码和HTML中的注释):

var clickGo = document.querySelector('.button-go');
var searchInput = document.querySelector('.search-input');
var currentValues = []; // keep track
clickGo.onclick = function () {
  var searchValue = searchInput.value;
  if (searchValue === "") { // check empty
    alert("please put a value in the input field");
  } else if (currentValues.indexOf(searchValue) == -1) { // check existing
     currentValues.push(searchValue);
     var liTag = document.createElement('li');
     liTag.textContent = searchValue;
     // append the new li-tag
     document.querySelector('[data-vals]').appendChild(liTag);
  } else { alert("value already exisits"); }
};
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}table{border-collapse:collapse;border-spacing:0}.container-header{background-color:#00628B}.row-header{min-height:120px;text-align:center;color:#fff;font-size:50px;font-family:oswald;padding-top:35px}.container-search-bar{background-color:#E6E6DC;min-height:75px}.container-search-inner .input-group{margin-top:20px}.container-content{padding-top:50px}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Search NOMO</title></title>
	  <link rel="stylesheet" href="assets/bootstrap/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/stylesheets/css/style.min.css">
</head>
<body>
   <div class="container-fluid container-wrapper">
      <div class="row row-wrapper">
         <div class="container-fluid container-header">
              <div class="row row-header">
                Search NOMO
              </div> <!-- row-header -->
         </div> <!-- container-header -->
         <div class="container-fluid container-search-bar">
             <div class="row row-search">
                <div class="container container-search-inner">
                  <div class="row">
                    <div class="col-lg-12">
                        <div class="input-group">
                          <input type="text" class="form-control search-input" placeholder="Add to List...">
                          <span class="input-group-btn">
                            <button class="btn btn-default button-go" type="button">Add</button>
                          </span>
                          <ul data-vals></ul><!-- values container -->
                        </div><!-- /input-group -->
                      </div><!-- /.col-lg-6 -->
                 </div> <!-- row -->
               </div> <!-- container-search-inner -->
            </div> <!-- row-search -->
        </div> <!-- container-search-bar -->
        <div class="container container-content">

        </div>
      </div> <!--row-wrapper -->
   </div> <!-- container-wrapper-->

   <!-- javascripts -->
   <script src="assets/jquery/dist/jquery.min.js"></script>
<script src="assets/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="assets/js/script.min.js"></script>

</body>
</html>

答案 1 :(得分:0)

  1. 移除您附加到window的点击事件监听器。这不是必需的。
  2. 尝试设置HTML元素的文本时,请勿使用appendChild。而是设置其textContent
  3. appendChild的点击处理程序中使用clickGocontainerContent附加新的li元素
  4. &#13;
    &#13;
    var clickGo = document.querySelector('.button-go');
    var searchInput = document.querySelector('.search-input');
    var containerContent = document.querySelector('.container-content');
    var listArray = [];
    clickGo.onclick = function() {
      var searchValue = searchInput.value;
      if (searchValue === "") {
        alert("please put a value in the input field");
    
      } else {
        //checking if array already contains added list item
        //if it is not, push the item to the array
        if (listArray.indexOf(searchValue) == -1) {
          listArray.push(searchValue);
    
          // Adding li tags to the array list items
          var liTag = document.createElement('li');
    
          for (var i = 0; i < listArray.length; i++) {
            liTag.textContent = listArray[i];
    
            containerContent.appendChild(liTag);
          }
          // if arry contains already contains list item then alert following message
        } else {
          alert("value already exisits");
        }
      }
    };
    &#13;
    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}table{border-collapse:collapse;border-spacing:0}.container-header{background-color:#00628B}.row-header{min-height:120px;text-align:center;color:#fff;font-size:50px;font-family:oswald;padding-top:35px}.container-search-bar{background-color:#E6E6DC;min-height:75px}.container-search-inner .input-group{margin-top:20px}.container-content{padding-top:50px}
    &#13;
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <title>Search NOMO</title>
      </title>
      <link rel="stylesheet" href="assets/bootstrap/dist/css/bootstrap.min.css">
      <link rel="stylesheet" href="assets/stylesheets/css/style.min.css">
    </head>
    
    <body>
      <div class="container-fluid container-wrapper">
        <div class="row row-wrapper">
          <div class="container-fluid container-header">
            <div class="row row-header">
              Search NOMO
            </div>
            <!-- row-header -->
          </div>
          <!-- container-header -->
          <div class="container-fluid container-search-bar">
            <div class="row row-search">
              <div class="container container-search-inner">
                <div class="row">
                  <div class="col-lg-12">
                    <div class="input-group">
                      <input type="text" class="form-control search-input" placeholder="Add to List...">
                      <span class="input-group-btn">
                                <button class="btn btn-default button-go" type="button">Add</button>
                              </span>
                    </div>
                    <!-- /input-group -->
                  </div>
                  <!-- /.col-lg-6 -->
                </div>
                <!-- row -->
              </div>
              <!-- container-search-inner -->
            </div>
            <!-- row-search -->
          </div>
          <!-- container-search-bar -->
          <div class="container container-content">
    
          </div>
        </div>
        <!--row-wrapper -->
      </div>
      <!-- container-wrapper-->
    
      <!-- javascripts -->
      <script src="assets/jquery/dist/jquery.min.js"></script>
      <script src="assets/bootstrap/dist/js/bootstrap.min.js"></script>
      <script src="assets/js/script.min.js"></script>
    
    </body>
    
    </html>
    &#13;
    &#13;
    &#13;

答案 2 :(得分:0)

我很抱歉,由于更改并使这项工作,我正在删除并添加内容并忘记在我粘贴的代码中添加UL标记,我正在尝试学习javascript,这是一个真正的帮助,如果您有关于学习java脚本的好网站和书籍的建议,请告诉我。

我觉得我会在这里发布很多东西以获得帮助

感谢您的耐心