首页 > 解决方案 > 如何在jquery中修改div内的html文档?

问题描述

如何div在 jQuery 中修改 HTML 文档?

这是我的代码:

$(".lastName").click(function() {
  val = $(".mydiv").clone();
  //then before to append the 'val' in result class, i want it first to modify the label text from 'First Name' to 'Last Name'
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mydiv">
  <label class="label">First Name</label>
  <input type="text" class="myinput">
</div>
<div class="result"></div>
<button class="lastName">Last Name</button

标签: javascripthtmljquery

解决方案


使用html(),但首先获取节点.find('.label')并打印结果$('.result').html(val)

$(".lastName").click(function() {
  let val = $(".mydiv").clone();
  $(val).find('.label').html('Last Name');
  //then before to append the 'val' in result class, i want it first to modify the label text from 'First Name' to 'Last Name'
  $('.result').html(val)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="mydiv">
  <label class="label">First Name</label>
  <input type="text" class="myinput">
</div>
<div class="result"></div>
<button class="lastName">Last Name</button>


推荐阅读