如何检查/删除元素?

时间:2016-09-21 12:26:23

标签: javascript jquery html css

$(function(){

  $("#tags input").on({
    focusout : function() {
      var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig,''); // allowed characters
      if(txt) $("<span/>", {text:txt.toLowerCase(), insertBefore:this});
      this.value = "";
    },
    keyup : function(ev) {
      // if: comma|enter (delimit more keyCodes with | pipe)
      if(/(188|13|32)/.test(ev.which)){
        $(this).focusout();
      } else if (/(8)/.test(ev.which) && this.value == '' && $(this).prev("span").css('background-color')=="rgb(225, 0, 0)") {
        $(this).prev("span").remove();
      } else if (/(8)/.test(ev.which) && this.value == '') {
        $(this).prev("span").css('background-color', 'Red');
      }
    }
  });
  $('#tags').on('click', 'span', function() {
     $(this).remove(); 
  });

});
#tags{
  float:right;
  border:1px solid #ccc;
  padding:5px;
  font-family:Arial;
}
#tags > span{
  cursor:pointer;
  display:block;
  float:right;
  color:#3e6d8e;
  background:#E1ECF4;
  padding:5px;
  padding-right:25px;
  margin:4px;
}
#tags > span:hover{
  opacity:0.7;
}
#tags > span:after{
 position:absolute;
 content:"×";
 border:1px solid;
 padding:2px 5px;
 margin-left:3px;
 font-size:11px;
}
#tags > input{
  direction: rtl;
  background:#eee;
  border:0;
  margin:4px;
  padding:7px;
  width:auto;
}
.autocomplete{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="tags">
  <span>php</span>
  <span>html</span>
  <input type="text" value="" placeholder="Add a tag" />
  <div class="autocomplete"></div>
</div>

我想通过按退格键(当输入聚焦时)或点击该标签来删除标签。现在我在谈论第一个选择。另外我需要先将它设置为红色背景颜色(因为“你确定吗?”概念)。好的,如你所见,我的小提琴设置为红色背景颜色,但它不会删除它。怎么了?

2 个答案:

答案 0 :(得分:1)

我在你的柜台内建立了一个检查你的按键状态的计数器。如果按一次计数器为1,如果按第二次,则删除元素,再次为0。我希望那就是你想要的。检查一下:

$(function() {

  counter = 0;

  $("#tags input").on({
    focusout: function() {
      var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig, ''); // allowed characters
      if (txt) $("<span/>", {
        text: txt.toLowerCase(),
        insertBefore: this
      });
      this.value = "";
      $("#tags span").css('background-color', '#E1ECF4');
      counter = 0;
    },
    keyup: function(ev) {
      // if: comma|enter (delimit more keyCodes with | pipe)
      if (/(188|13|32)/.test(ev.which)) {
        $(this).focusout();
      } else if (/(8)/.test(ev.which) && this.value == '') {
        counter = counter + 1;
        if (counter == 1) {
          $(this).prev("span").css('background-color', 'Red');
        } else {
          $(this).prev("span").remove();
          counter = 0;
        }
      }
    }
  });
  $('#tags').on('click', 'span', function() {
    $(this).remove();
      $("#tags span").css('background-color', '#E1ECF4');
      counter = 0;
  });

});

https://jsfiddle.net/4swtduae/7/

答案 1 :(得分:1)

您最好切换课程并检查它,例如:

&#13;
&#13;
$(function() {

  $("#tags input").on({
    focusout: function() {
      var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig, ''); // allowed characters
      if (txt) $("<span/>", {
        text: txt.toLowerCase(),
        insertBefore: this
      });
      this.value = "";
    },
    keyup: function(ev) {
      // if: comma|enter (delimit more keyCodes with | pipe)
      if (/(188|13|32)/.test(ev.which)) {
        $(this).focusout();
      } else if (/(8)/.test(ev.which) && this.value == '' && $(this).prev("span").hasClass('toRemove')) { //<< check for class
        $(this).prev("span").remove();
      } else if (/(8)/.test(ev.which) && this.value == '') {
        $(this).prev("span").addClass('toRemove'); //<< add class
      } else {
        $(this).prevAll('.toRemove').removeClass('toRemove'); //<< remove class on keyup
      }
    }
  });
  $('#tags').on('click', 'span', function() {
    $(this).remove();
  });

});
&#13;
#tags {
  float: right;
  border: 1px solid #ccc;
  padding: 5px;
  font-family: Arial;
}
#tags > span {
  cursor: pointer;
  display: block;
  float: right;
  color: #3e6d8e;
  background: #E1ECF4;
  padding: 5px;
  padding-right: 25px;
  margin: 4px;
}
#tags > span:hover {
  opacity: 0.7;
}
#tags > span:after {
  position: absolute;
  content: "×";
  border: 1px solid;
  padding: 2px 5px;
  margin-left: 3px;
  font-size: 11px;
}
#tags > input {
  direction: rtl;
  background: #eee;
  border: 0;
  margin: 4px;
  padding: 7px;
  width: auto;
}
.autocomplete {
  display: none;
}
#tags > span.toRemove {
  background-color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="tags">
  <span>php</span>
  <span>html</span>
  <input type="text" value="" placeholder="Add a tag" />
  <div class="autocomplete"></div>
</div>
&#13;
&#13;
&#13;