自动调整大小的文本元素不能与键盘配合使用

时间:2019-03-29 16:48:22

标签: javascript jquery input keypress keyup

在下面的jQuery中,我有一个元素,该元素反映了辅助元素中的书面input。同时具有反射文本的元素需要调整大小,这是可行的,但是有两个我无法解决的问题:

1。按住退格键时,它不会跟上。
2。当您按单个退格键时,它将跳过一个字符,并且无法清空字段。

请参见以下代码段:

jQuery(document).ready( function() {
    jQuery(function(){
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    }).on('input', function () {
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    });
    jQuery('#vanity-url').bind('keypress keyup blur', function() {
        jQuery('#reflection').val(jQuery(this).val());
    });
});
body {
  background-color: #e4e4e4;
  font-family: Arial;
}

#vanity-url-wrapper {
  margin-top: 3em;
  text-align: center;
}

#vanity-url-wrapper > span {
  background-color: #FBE3CF;
  border-radius: 8px;
  padding: 0.5em 0;
  border: 2px solid orange;
}

.pre-span {
  background-color: orange;
  color: white;
  font-weight: bold;
  padding: 0.5em;
}

#vanity-url {
  display: block;
  text-align: center;
  width: 12em;
  margin: 3em auto;
  font-size: 1.2em;
  border-radius: 5px;
  border: 2px solid orange;
  padding: 0.5em;
}

#vanity-span{
  padding: 0.5em;
}

#reflection {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
  <div id="vanity-url-wrapper">
    <span>
      <span class="pre-span">https://example.com/</span>   
      <span id="vanity-span"></span>
      <input id="reflection" type="text" readonly>
      <span class="pre-span">/</span>
    </span>
  </div>
  <input id="vanity-url" type="text" placeholder="Type here your vanity URL">
</body>

1 个答案:

答案 0 :(得分:1)

问题是.bind('keypress keyup blur', function() {不能很好地与更新值配合。按下键时,它需要更新并等待按下,然后跳过,反之亦然。

所以这里的解决方案是改用.on('input', function() {

请参见以下结果:

jQuery(document).ready( function() {
    jQuery(function(){
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    }).on('input', function () {
        jQuery('#vanity-span').text(jQuery('#reflection').val());
        jQuery('#reflection').width(jQuery('span').width());
    });
    jQuery('#vanity-url').on('input', function() {
        jQuery('#reflection').val(jQuery(this).val());
    });
});
body {
  background-color: #e4e4e4;
  font-family: Arial;
}

#vanity-url-wrapper {
  margin-top: 3em;
  text-align: center;
}

#vanity-url-wrapper > span {
  background-color: #FBE3CF;
  border-radius: 8px;
  padding: 0.5em 0;
  border: 2px solid orange;
}

.pre-span {
  background-color: orange;
  color: white;
  font-weight: bold;
  padding: 0.5em;
}

#vanity-url {
  display: block;
  text-align: center;
  width: 12em;
  margin: 3em auto;
  font-size: 1.2em;
  border-radius: 5px;
  border: 2px solid orange;
  padding: 0.5em;
}

#vanity-span{
  padding: 0.5em;
}

#reflection {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body>
  <div id="vanity-url-wrapper">
    <span>
      <span class="pre-span">https://example.com/</span>   
      <span id="vanity-span"></span>
      <input id="reflection" type="text" readonly>
      <span class="pre-span">/</span>
    </span>
  </div>
  <input id="vanity-url" type="text" placeholder="Type here your vanity URL">
</body>

相关问题