【问题标题】:Auto resize text element not working with keyup自动调整文本元素的大小不适用于 keyup
【发布时间】:2019-08-20 15:59:51
【问题描述】:

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

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

请看下面的sn-p:

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>

【问题讨论】:

    标签: javascript jquery input keypress keyup


    【解决方案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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 2011-04-22
      • 1970-01-01
      • 2011-09-08
      • 2011-05-13
      • 1970-01-01
      • 2017-03-09
      相关资源
      最近更新 更多