javascript - 无法访问 jQuery 插入的元素

标签 javascript jquery input insert click

<分区>

我给自己做了一个小标签输入字段,但我无法使用 jquery 访问插入的标签。

每次您点击回车时,输入字段的值将作为 <div class="chip"></div> 插入到输入之前.

如果我尝试在点击时执行某些操作,它仅适用于输入未插入的标签。

$('.chip').on('click', function() {
  console.log('click');
});

$('.wrap input').on('keydown', function(e) {
  var key = e.which,
      input = $(this),
      value = input.val();
	
  if(key === 13) {
    if(value != '') {
      $('<div class="chip">' + value + '</div>').insertBefore(input);
      $(input).val('');
    }
  }
});
.wrap {
  box-sizing: border-box;
  width: 80%;
  height: 55px;
  line-height: 55px;
  border: 1px solid #bebebe;
  border-radius: 28px;
  padding: 0 .5rem;
  font-size: 1rem;
  font-weight: 400;
  display: flex;
  flex-direction: row;
  align-items: center;
  overflow: auto;
}

.wrap .chip {
  font-size: 14px;
  font-weight: 300;
  background: #e6e6e6;
  border-radius: 20px;
  height: 40px;
  padding: 0 20px;
  line-height: 40px;
  margin-right: 5px;
}

.wrap input {
  box-sizing: border-box;
  height: 40px;
  line-height: 40px;
  padding: 0 1rem;
  font-size: 1rem;
  font-weight: 400;
  border: none;
  flex: 1;
}

.wrap input:focus {
  outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <div class="chip">tag1</div>
  <input type="text">
</div>

最佳答案

$('.wrap').on('click', '.chip', function() {
  console.log('click');
});

$('.wrap input').on('keydown', function(e) {
  var key = e.which,
      input = $(this),
      value = input.val();
	
  if(key === 13) {
    if(value != '') {
      $('<div class="chip">' + value + '</div>').insertBefore(input);
      $(input).val('');
    }
  }
});
.wrap {
  box-sizing: border-box;
  width: 80%;
  height: 55px;
  line-height: 55px;
  border: 1px solid #bebebe;
  border-radius: 28px;
  padding: 0 .5rem;
  font-size: 1rem;
  font-weight: 400;
  display: flex;
  flex-direction: row;
  align-items: center;
  overflow: auto;
}

.wrap .chip {
  font-size: 14px;
  font-weight: 300;
  background: #e6e6e6;
  border-radius: 20px;
  height: 40px;
  padding: 0 20px;
  line-height: 40px;
  margin-right: 5px;
}

.wrap input {
  box-sizing: border-box;
  height: 40px;
  line-height: 40px;
  padding: 0 1rem;
  font-size: 1rem;
  font-weight: 400;
  border: none;
  flex: 1;
}

.wrap input:focus {
  outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
  <div class="chip">tag1</div>
  <input type="text">
</div>

事件仅绑定(bind)到 DOM 中可用的元素。为了说明新插入的 dom 节点,您必须将事件委托(delegate)给在绑定(bind)事件时可用的祖先。

改变

$('.chip').on('click', function() {
  console.log('click');
});

$('body').on('click', '.chip', function() {
  console.log('click');
});

body 可以替换为绑定(bind)时 DOM 中最接近的祖先。

关于javascript - 无法访问 jQuery 插入的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50807218/

相关文章:

javascript - 使用 jquery 获取一组复选框值

javascript - Vue Draggable 添加事件拾取不正确的项目

javascript - 如何使用存储在变量中的值调用函数?

HTML5 模式允许特定的特殊字符

python - Python 3 中是否有类似于 C++ 中的 getchar() 的内置函数?

javascript - 维基百科 API 不返回请求 ID

javascript - 如何停止数组中的项目在显示后重复

javascript - 使用 jQuery Slider 使用淡入淡出方式擦洗图像

javascript - 从底部开始将元素附加到 div?

java - 让java多线程等待输入给定