JavaScript 更改元素类型

标签 javascript jquery html css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 4 年前

我有一个按钮,我希望当我点击它时添加一个输入,然后按下回车输入按钮或其他东西。


<button>add tag</button>
<input type="text" name="" placeholder="tag">

$(document).ready(function () {
        $('button').click(function () {
            $("button").before("input");
        });
    });

最佳答案

试试这个

$(document).ready(function() {
  $('button').click(function() {
    $("body").before("input");
  });
  $("input").keyup(function(e) {
    if (e.keyCode == 13)
      $(this).attr("type", "button");
    $(this).val('button')
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag"><button>add tag</button>
<input type="text" name="" placeholder="tag">

关于JavaScript 更改元素类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842240/

上一篇:html - 不良做法 HTML/CSS 包装器

下一篇:php - 链接在 PHP 中不起作用或无法正确显示

相关文章:

javascript - jQueryeach() 不工作

php - 如何根据相同的类而不是相同的名称对单选按钮进行分组

javascript - 如何在开 Jest 中获得最近一次对模拟函数的调用?

javascript - Scrapy 选择器无法返回所需的字符,可能是由于 Javascript

javascript - 未插入数据库表的值

javascript - IndexedDB 未保存在数据库中

jquery - 由于自定义 CSS,Bootstrap 响应式导航栏折叠按钮无法正常工作

html - 文本自动滚动 div 提前停止

html - 悬停时隐藏标题标签

Javascript - 动态添加的选项卡在 Kendo 面板栏中不显示为选项卡