经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
可编辑且宽度自适应input
来源:cnblogs  作者:谷惊雨蛰  时间:2019/3/21 8:49:43  对本文有异议

默认的input项是比较难看的,并且它的宽度还无法随着输入而变化,这样未免有些呆板,不过借助JavaScript可以达到宽度自适应的效果,下面为了方便使用了jQuery:

  1. <div class="list">
  2. <span class="list_name">娉娉袅袅</span>
  3. <input type="text" class="list_name hidden">
  4. </div>
  1. .hidden{
  2. display: none;
  3. }
  4. .list{
  5. background-color: #303030;
  6. color: aliceblue;
  7. display: inline-block;
  8. font-size: 2rem;
  9. padding: 0.5rem 1.5rem;
  10. border-radius: 1rem;
  11. }
  12. input{
  13. border: none;
  14. outline: none;
  15. font-size: 2rem;
  16. background-color: transparent;
  17. color: #F16665;
  18. caret-color: aliceblue;
  19. }
  1. var $span = $("span.list_name");
  2. var $input = $("input.list_name");
  3. function onEdit($span){
  4. $span.hide()
  5. .siblings("input.list_name")
  6. .val($span.text())
  7. .show()
  8. .focus();
  9. }
  10. $span.click(function(){
  11. onEdit($(this));
  12. });
  13. $input.bind("keydown", function(e){
  14. if(e.keyCode == "13"){
  15. $(this).hide()
  16. .siblings("span.list_name")
  17. .text($(this).val())
  18. .show();
  19. }
  20. })
  21. $input.bind("blur", function(){
  22. $(this).hide()
  23. .siblings("span.list_name")
  24. .text($(this).val())
  25. .show();
  26. });
  27. $("input[type='text']").bind("focus",function(){
  28. $(this).css("width",$(this).val().length + "em");
  29. })
  30. $("input[type='text']").bind("input propertychange",function(){
  31. $(this).css("width",$(this).val().length + "em");
  32. })

点击此查看样例

原文链接:http://www.cnblogs.com/lotusloli/p/10567616.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号