javascript - 如何在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
解决方案
使用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>
推荐阅读
- ios - 在 didFinishLaunchingWithOptions 检查无效的用户令牌火力库
- windows - 如何在 Windows EC2 实例上运行 Minikube?
- python - 当模型位于版本子目录中时,Django 转储数据返回空数组
- android - 应用关闭时,Android WearableListenerService 不会在 Android 8 Oreo 上触发
- ocaml - 此 OCaml 表达式在未定义为 AFAIK 时具有其参数的类型,为什么此其他表达式具有 '_a 而不是 'a?
- python - Selenium 节点在打开后不久崩溃
- php - WordPress 远程请求发布问题
- python - Conda 想在卸载时降级不相关的包
- javascript - 内容通过点击链接动画化
- amcharts - AMCharts - 设置 Y 轴的标签