gpt4 book ai didi

javascript - 自动调整文本元素大小不适用于 keyup

转载 作者:行者123 更新时间:2023-11-30 19:40:47 25 4
gpt4 key购买 nike

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

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>

最佳答案

问题是 .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>

关于javascript - 自动调整文本元素大小不适用于 keyup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55422036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com