document.getElementById("XXX").innerHTML与document.getElementById("XXX").value

本文介绍了解决从HTML元素中获取文本时遇到的特殊字符转义问题,提供了两种有效的方法来还原原始文本内容,包括使用自定义函数进行转换及直接通过.value属性获取。

工作中碰到一个问题,以document.getElementById("XXX").innerHTML获取某个textarea中的值时发现:>总是变成>。经过查资料,了解到document.getElementById("XXX").innerHTML方式获取值的时候会作如下转换:

<转换成&lt;

>转换成&gt;

&转换成&amp;

"转换成&quot;

可按如下两种方法处理这个问题:

方法一:

//转意符换成普通字符

function escape2Html(str) {
    var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
}

方法二:

//使用document.getElementById("XXX").value获取值

document.getElementById("XXX").value;

后端不改动,根据<script> const bucketSelect = document.getElementById("bucket-select"); const dirSelectors = document.getElementById("dir-selectors"); const jsonlSelect = document.getElementById("jsonl-select"); const datasetTypeLabel = document.getElementById("dataset-type-label"); const datasetType = document.getElementById("dataset-type"); const loadBtn = document.getElementById("load-btn"); const imagePreview = document.getElementById("image-preview"); const conversationDiv = document.getElementById("conversation"); const imageNav = document.getElementById("image-nav"); const prevBtn = document.getElementById("prev-btn"); const nextBtn = document.getElementById("next-btn"); let jsonlData = []; let currentImageIndex = 0; let jsonlDir = ""; // 存储 JSONL 文件所在目录路径 // 获取桶列表 fetch("/buckets") .then(res => res.json()) .then(buckets => { buckets.forEach(b => { const opt = document.createElement("option"); opt.value = b; opt.textContent = b; bucketSelect.appendChild(opt); }); }); // 桶变化时,清空目录选择器并初始化第一级目录 bucketSelect.addEventListener("change", () => { dirSelectors.innerHTML = ""; jsonlSelect.innerHTML = "<option value=''>请选择文件</option>"; datasetTypeLabel.style.display = "none"; datasetType.style.display = "none"; datasetType.value = ""; loadBtn.style.display = "none"; loadBtn.disabled = true; const bucket = bucketSelect.value; if (!bucket) return; // 获取第一级目录 fetch(`/directories?bucket=${bucket}`) .then(res => res.json()) .then(dirs => { if (dirs.length) { createDirectorySelector(bucket, dirs, 0); } }); }); // 创建目录选择器(支持多级) function createDirectorySelector(bucket, dirs, level) { const container = document.createElement("div"); const select = document.createElement("select"); select.innerHTML = "<option value=''>请选择目录</option>"; dirs.forEach(d => { const opt = document.createElement("option"); opt.value = d; opt.textContent = d; select.appendChild(opt); }); container.appendChild(select); dirSelectors.appendChild(container); // 创建 JSONL 文件选择器 const jsonlContainer = document.createElement("div"); jsonlContainer.innerHTML = ` <label>本目录 JSONL 文件:</label> <select class="jsonl-select" style="margin-bottom: 10px;"> <option value=''>加载中...</option> </select> `; dirSelectors.appendChild(jsonlContainer); const currentJsonlSelect = jsonlContainer.querySelector(".jsonl-select"); // 获取当前目录下的 JSONL 文件 fetch(`/jsonl_files?bucket=${bucket}&prefix=${select.value}`) .then(res => res.json()) .then(files => { currentJsonlSelect.innerHTML = "<option value=''>请选择文件</option>"; if (files.length) { files.forEach(file => { const opt = document.createElement("option"); opt.value = file; opt.textContent = file; currentJsonlSelect.appendChild(opt); }); } else { currentJsonlSelect.innerHTML = "<option value=''>无 JSONL 文件</option>"; } }); // JSONL 文件选择事件 currentJsonlSelect.addEventListener("change", () => { const file = currentJsonlSelect.value; if (!file) return; // 清除后续所有选择器 while (container.nextSibling) { dirSelectors.removeChild(container.nextSibling); } datasetTypeLabel.style.display = "inline-block"; datasetType.style.display = "inline-block"; datasetType.value = ""; loadBtn.style.display = "inline-block"; loadBtn.disabled = false; // 设置隐藏的 jsonl-select jsonlSelect.innerHTML = `<option value="${file}" selected>${file}</option>`; }); // 目录选择事件 select.addEventListener("change", () => { const prefix = select.value; if (!prefix) return; // 清除后续所有选择器 while (container.nextSibling) { dirSelectors.removeChild(container.nextSibling); } // 获取下一级目录 fetch(`/subdirectories?bucket=${bucket}&prefix=${prefix}`) .then(res => res.json()) .then(subDirs => { if (subDirs.length) { createDirectorySelector(bucket, subDirs, level + 1); } }); }); } // 加载按钮点击事件 loadBtn.addEventListener("click", () => { const bucket = bucketSelect.value; const file = jsonlSelect.value; const type = datasetType.value; if (!bucket || !file || !type) return; jsonlDir = file.substring(0, file.lastIndexOf('/') + 1); fetch(`/jsonl?bucket=${bucket}&file=${file}`) .then(res => res.json()) .then(data => { jsonlData = data; currentImageIndex = 0; if (jsonlData.length > 0) { if (type === "single_image_detection") { displaySingleImageDetection(jsonlData[currentImageIndex]); imageNav.style.display = "flex"; } else if (type === "multi_image_conversation") { conversationDiv.innerHTML = "<p>多图对话任务展示方式待实现</p>"; } else if (type === "video_annotation") { conversationDiv.innerHTML = "<p>视频标注功能待实现</p>"; } } }); }); function displaySingleImageDetection(item) { const imagePath = jsonlDir + item.src; fetch(`/image?bucket=${bucketSelect.value}&path=${imagePath}`) .then(res => res.blob()) .then(blob => { imagePreview.src = URL.createObjectURL(blob); }); conversationDiv.innerHTML = ""; item.conversation.forEach(msg => { const div = document.createElement("div"); div.className = "message " + (msg.from === "human" ? "human" : "gpt"); div.textContent = `${msg.from}: ${msg.value.replace(/\n/g, '')}`; conversationDiv.appendChild(div); }); } // 图片导航 prevBtn.addEventListener("click", () => { if (currentImageIndex > 0) { currentImageIndex--; displaySingleImageDetection(jsonlData[currentImageIndex]); } }); nextBtn.addEventListener("click", () => { if (currentImageIndex < jsonlData.length - 1) { currentImageIndex++; displaySingleImageDetection(jsonlData[currentImageIndex]); } }); </script> 这个前端给我一版新的前端代码
08-12
<script> const bucketSelect = document.getElementById("bucket-select"); const dirSelectors = document.getElementById("dir-selectors"); const jsonlSelect = document.getElementById("jsonl-select"); const datasetTypeLabel = document.getElementById("dataset-type-label"); const datasetType = document.getElementById("dataset-type"); const loadBtn = document.getElementById("load-btn"); const imagePreview = document.getElementById("image-preview"); const conversationDiv = document.getElementById("conversation"); const imageNav = document.getElementById("image-nav"); const prevBtn = document.getElementById("prev-btn"); const nextBtn = document.getElementById("next-btn"); let jsonlData = []; let currentImageIndex = 0; let jsonlDir = ""; // 存储 JSONL 文件所在目录路径 // 获取桶列表 fetch("/buckets") .then(res => res.json()) .then(buckets => { buckets.forEach(b => { const opt = document.createElement("option"); opt.value = b; opt.textContent = b; bucketSelect.appendChild(opt); }); }); // 桶变化时,清空目录选择器并初始化第一级目录 bucketSelect.addEventListener("change", () => { dirSelectors.innerHTML = ""; jsonlSelect.innerHTML = "<option value=''>请选择文件</option>"; datasetTypeLabel.style.display = "none"; datasetType.style.display = "none"; datasetType.value = ""; loadBtn.style.display = "none"; loadBtn.disabled = true; const bucket = bucketSelect.value; if (!bucket) return; // 获取第一级目录 fetch(`/directories?bucket=${bucket}`) .then(res => res.json()) .then(dirs => { if (dirs.length) { createDirectorySelector(bucket, dirs, 0); } }); }); // 创建目录选择器(支持多级) function createDirectorySelector(bucket, dirs, level) { const container = document.createElement("div"); const select = document.createElement("select"); select.innerHTML = "<option value=''>请选择目录</option>"; dirs.forEach(d => { const opt = document.createElement("option"); opt.value = d; opt.textContent = d; select.appendChild(opt); }); container.appendChild(select); dirSelectors.appendChild(container); // 创建 JSONL 文件选择器 const jsonlContainer = document.createElement("div"); jsonlContainer.innerHTML = ` <label>本目录 JSONL 文件:</label> <select class="jsonl-select" style="margin-bottom: 10px;"> <option value=''>加载中...</option> </select> `; dirSelectors.appendChild(jsonlContainer); const currentJsonlSelect = jsonlContainer.querySelector(".jsonl-select"); // 获取当前目录下的 JSONL 文件 fetch(`/jsonl_files?bucket=${bucket}&prefix=${select.value}`) .then(res => res.json()) .then(files => { currentJsonlSelect.innerHTML = "<option value=''>请选择文件</option>"; if (files.length) { files.forEach(file => { const opt = document.createElement("option"); opt.value = file; opt.textContent = file; currentJsonlSelect.appendChild(opt); }); } else { currentJsonlSelect.innerHTML = "<option value=''>无 JSONL 文件</option>"; } }); // JSONL 文件选择事件 currentJsonlSelect.addEventListener("change", () => { const file = currentJsonlSelect.value; if (!file) return; // 清除后续所有选择器 while (container.nextSibling) { dirSelectors.removeChild(container.nextSibling); } datasetTypeLabel.style.display = "inline-block"; datasetType.style.display = "inline-block"; datasetType.value = ""; loadBtn.style.display = "inline-block"; loadBtn.disabled = false; // 设置隐藏的 jsonl-select jsonlSelect.innerHTML = `<option value="${file}" selected>${file}</option>`; }); // 目录选择事件 select.addEventListener("change", () => { const prefix = select.value; if (!prefix) return; // 清除后续所有选择器 while (container.nextSibling) { dirSelectors.removeChild(container.nextSibling); } // 获取下一级目录 fetch(`/subdirectories?bucket=${bucket}&prefix=${prefix}`) .then(res => res.json()) .then(subDirs => { if (subDirs.length) { createDirectorySelector(bucket, subDirs, level + 1); } }); }); } // 加载按钮点击事件 loadBtn.addEventListener("click", () => { const bucket = bucketSelect.value; const file = jsonlSelect.value; const type = datasetType.value; if (!bucket || !file || !type) return; jsonlDir = file.substring(0, file.lastIndexOf('/') + 1); fetch(`/jsonl?bucket=${bucket}&file=${file}`) .then(res => res.json()) .then(data => { jsonlData = data; currentImageIndex = 0; if (jsonlData.length > 0) { if (type === "single_image_detection") { displaySingleImageDetection(jsonlData[currentImageIndex]); imageNav.style.display = "flex"; } else if (type === "multi_image_conversation") { conversationDiv.innerHTML = "<p>多图对话任务展示方式待实现</p>"; } else if (type === "video_annotation") { conversationDiv.innerHTML = "<p>视频标注功能待实现</p>"; } } }); }); function displaySingleImageDetection(item) { const imagePath = jsonlDir + item.src; fetch(`/image?bucket=${bucketSelect.value}&path=${imagePath}`) .then(res => res.blob()) .then(blob => { imagePreview.src = URL.createObjectURL(blob); }); conversationDiv.innerHTML = ""; item.conversation.forEach(msg => { const div = document.createElement("div"); div.className = "message " + (msg.from === "human" ? "human" : "gpt"); div.textContent = `${msg.from}: ${msg.value.replace(/\n/g, '')}`; conversationDiv.appendChild(div); }); } // 图片导航 prevBtn.addEventListener("click", () => { if (currentImageIndex > 0) { currentImageIndex--; displaySingleImageDetection(jsonlData[currentImageIndex]); } }); nextBtn.addEventListener("click", () => { if (currentImageIndex < jsonlData.length - 1) { currentImageIndex++; displaySingleImageDetection(jsonlData[currentImageIndex]); } }); </script> 选择桶之后,选择目录,然后可以选择下一级目录,也可以选择当前目录下的jsonl,选择下一级目录之后,把选择的结果传给选择目录框,可以继续再选择下一级目录选择下一级目录,若是选择了当前目录下jsonl之后,就开始选择数据集类型了,给我一般尽量精简的前端代码
08-09
// 会员管理 searchMember() { const cardNumber = document.getElementById('memberCardInput').value.trim() if (!cardNumber) return // 模拟会员查询 const member = { // id: cardNumber, // name: '张三', // phone: '138****1234', // points: 1250, // level: 'VIP' } $.ajax({ type: 'POST', async: false, url: '/updateMemberBalance.action', data: { mobile: cardNumber }, dataType: 'json', success: msg => { document.getElementById('memberCardInput').value = '' if (msg.statusCode == 200) { this.currentMember = { id: msg.data.memberBalance.mobile, name: msg.data.memberBalance.memberName, points: msg.data.memberBalance.balance, } this.showMemberInfo() this.hideModal('memberModal') this.showNotification(`余额查询登录成功`, 'success') } else { this.showNotification('卡不存在', 'error') this.playPaymentSound("卡不存在") } }, error: msg => { this.showNotification('卡不存在', 'error') this.playPaymentSound("卡不存在") } }) } showMemberInfo() { if (this.currentMember) { const memberInfo = document.getElementById('memberInfo') memberInfo.style.display = 'block' memberInfo.querySelector('.member-name').textContent = this.currentMember.name memberInfo.querySelector( '.member-id' ).textContent = `卡号:${this.currentMember.id}` memberInfo.querySelector( '.member-points' ).textContent = `余额:${this.currentMember.points}` } } removeMember() { this.currentMember = null document.getElementById('memberInfo').style.display = 'none' }具体分析什么功能
最新发布
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值