<分区>
我给自己做了一个小标签输入字段,但我无法使用 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>