,可以通过以下步骤实现:
以下是一个示例代码:
HTML部分:
<div id="targetDiv">这是目标div的文本内容</div>
<input type="text" id="textInput" placeholder="表单输入">
<button onclick="retrieveText()">检索并放入表单输入</button>
JavaScript部分:
function retrieveText() {
// 获取目标div的文本内容
var targetDiv = document.getElementById("targetDiv");
var textContent = targetDiv.innerText;
// 将文本内容放入表单输入
var textInput = document.getElementById("textInput");
textInput.value = textContent;
}
在上述示例中,点击按钮时会调用retrieveText()函数,该函数会获取目标div的文本内容,并将其放入id为textInput的表单输入中。
这个方法适用于需要将某个元素的文本内容作为默认值放入表单输入中的场景,例如在编辑页面中显示已有的文本内容。
jquery
[] jquery概念
jquery是JS的框架。
JS的函数库。
【】BOM
BOM:Browser Object Model
BOM对象:
1.window:BOM根对象
2.window.navigator 浏览器对象
3.window.location : URL地址对象
4.window.document: 文档对象。
5.window.history 历史对象
【】DOM
DOM根对象:window.document 表示浏览器载入的文档在内存中模型。
DOM模式的格式:树。
每个标记表示一个对象,在树中是一个节点。
1. JS定位一个节点方法
(1)根据ID定位:var div=document.getElementById("id"); 返回一个对象
(2)根据标记名定义:var div=document.getElemenetByTagName("div");
返回对象的数组。
(3) 根据CSS选择器选择对象:
var ob=document.querySelector("css选择器");返回满足选择器的第一个对象
例子:
<input type="text" name="userid" id="userid" />
var userid=document.querySelector("input[name='userid']");
var userid=document.querySelector("#userid");
var userid=document.querySelector("input");
(4) 返回所有的选择器选择的对象:返回对象数组。
document.querySelectorAll("CSS选择器")
【】DOM操作节点对象
1.读/写节点的内容
领取专属 10元无门槛券
手把手带您无忧上云