javascript - 输入小于 100px 时换行,显示 flex

标签 javascript html css flexbox line-breaks

我正在做一个带有标签的搜索框,但是当我添加超过 X 个标签时没有换行符。

输入是 100% - “标签元素”(宽度:-webkit-fill-available;)但是我想在输入小于 100px 时换行,例如,因为现在输入字段消失了“inputholder”框。

这里有一个示例代码(如果您在输入框中按回车键,标签元素将添加到输入框):

这是我的HTML:

$(".search-input").keypress(function(e) {
  if (e.which == 13) {
    $('#inputtext').before('<span class="tag">test-tag</span>');
    $('.search-input').val('');
  }
});
.inputholder {
  /*border:1px solid black;*/
  display: flex;
  align-items: baseline;
  padding-left: 5px;
  float: left;
  background: #FAFAFA;
  border: 1px solid #ccc;
  color: #8D9BAF;
  width: 70%;
  /*padding:10px;*/
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}

.location-tag {
  cursor: pointer;
  padding: 0px;
  width: 20%;
}

.tag {
  padding: 7px;
  background-color: #AAA;
  border-radius: 20px;
  font-size: 15px;
  margin-right: 2px;
  color: white;
  text-align: center;
  white-space: nowrap;
  /*display: table-cell;*/
}

.tag-selected {
  background: #424242;
}

.inputholder input {
  border: 0 !important;
  padding: 12px !important;
  width: -webkit-fill-available;
  float: right;
  padding: 10px;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  background: transparent !important;
  color: #8D9BAF;
}

.inputholder input:focus {
  outline-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="inputholder">
  <span class="tag location-tag">
        <i class="fas fa-map-marker-alt"></i> Ubicación
    </span>
  <input type="text" class="search-input" id="inputtext" />
</div>

最佳答案

$(".search-input").keypress(function(e) {
  if (e.which == 13) {
    $('#inputtext').before('<span class="tag">test-tag</span>');
    $('.search-input').val('');
  }
});
.inputholder {
  /*border:1px solid black;*/
  /*display: flex;*/
  align-items: baseline;
  padding-left: 5px;
  float: left;
  background: #FAFAFA;
  border: 1px solid #ccc;
  color: #8D9BAF;
  width: 70%;
  /*padding:10px;*/
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
}

.location-tag {
  cursor: pointer;
  padding: 0px;
  width: 20%;
}

.tag {
display:inline-flex;
  padding: 7px;
  background-color: #AAA;
  border-radius: 20px;
  font-size: 15px;
  margin-right: 2px;
  color: white;
  text-align: center;
  white-space: nowrap;
  /*display: table-cell;*/
}

.tag-selected {
  background: #424242;
}

.inputholder input {
  border: 0 !important;
  padding: 12px !important;
  width: -webkit-fill-available;
  float: right;
  padding: 10px;
  border-top-left-radius: 20px;
  border-bottom-left-radius: 20px;
  background: transparent !important;
  color: #8D9BAF;
}

.inputholder input:focus {
  outline-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="inputholder">
  <span class="tag location-tag">
        <i class="fas fa-map-marker-alt"></i> Ubicación
    </span>
  <input type="text" class="search-input" id="inputtext" />
</div>

关于javascript - 输入小于 100px 时换行,显示 flex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53537314/

相关文章:

javascript - 引用错误 : event is not defined error in Firefox

javascript - 试图整理出 JQuery 的算法

html - 垂直对齐导航

javascript - 每次单击链接时都会执行 .js 文件

javascript - element.addEventListener(event, action, false) 中 false 的目的是什么?

javascript - AngularJS 地理位置数据更新

python - 使用 docutils.core.publish_string 而不是 publish_cmdline,携带 HTML-Specific Options 的 `settings_overrides` 参数无效!

html - png图像路径后这些数字是什么意思?

html - 改变内联样式 TWebbrower Delphi

jquery 切换星形脚本