【问题标题】:how to change placement of input and its value with jquery如何使用 jquery 更改输入的位置及其值
【发布时间】:2023-01-30 23:40:04
【问题描述】:

我正在使用 .html() 获取 div 内的输入,然后再次使用 .html() 将其放置在另一个元素中,问题是我无法获取它的值。

$("#myButton").click(function(){
    $(".element2").html($(".element1").html());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js</script>

<div class="element1">
    <input type="text" placeholder="enter something then clone it!"/>
</div>
<div class="element2"></div>
<button id="myButton">click to clone</button>

【问题讨论】:

  • 如果你想移动一个 DOM 元素(由问题标题和文本暗示)然后使用 .append() / .appendTo() - 如果你想复制一个元素(如代码中的文本所暗示的)然后使用.clone()

标签: html jquery


【解决方案1】:

复制一个元素到另一个元素,你可以使用.clone()

var copy = $(".element1 > *").clone()

这将使用 .element1 克隆所有元素 - 然后您可以将这些元素 .append()/.appendTo() 复制到 element2。

更新了 SN-P:

$("#myButton").click(function(){
    $(".element1 > *").clone().appendTo(".element2");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="element1">
    <input type="text" placeholder="enter something then clone it!"/>
</div>

<div class="element2"></div>
<button id="myButton">click to clone</button>

【讨论】:

  • 哦,谢谢,它会完成这项工作。
猜你喜欢
  • 2023-03-05
  • 1970-01-01
  • 2019-04-05
  • 2019-02-26
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多