首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动调整不使用keyup的文本元素的大小

自动调整不使用keyup的文本元素的大小
EN

Stack Overflow用户
提问于 2019-03-29 16:48:22
回答 1查看 84关注 0票数 1

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

  1. 当按住一个后座时,它就跟不上了。
  2. 当您按下单个退格时,它会跳过一个字符,并且无法清空字段。

请参阅下面的片段:

代码语言:javascript
复制
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());
    });
});
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-29 23:58:50

问题是,.bind('keypress keyup blur', function() {没有很好地配合更新值。当键关闭时,它需要更新并等待向上,然后跳过,反之亦然。

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

见下文成果:

代码语言:javascript
复制
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());
    });
});
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55422036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档