You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SVG内输入框聚焦时出现黑色异常问题求助

SVG文本可编辑功能实现问题

我正在绘制包含三个矩形及对应文本标签的SVG,需要实现文本可编辑功能。尝试使用contenteditable属性(未生效)和input框(仅部分生效),但input框聚焦时会出现黑色异常,且无法调试(点击检查元素会失焦,黑色消失),调整字体也无效。

相关代码如下:

const drawSvg= svg.append("g")
 .append("rect")
.attr("id", "myRect")
 .style("fill", function (d) {color});
g.selectAll("g").append("text").attr("id", "myText").on("click", textClick)
.text (function (d) {return label;});
g.selectAll("g").append("foreignObject")

function textClick(evt) {
let originalText = evt.target.innerHTML
const input = document.createElement("input");
input.value = originalText;
input.onkeyup = function(e) {
evt.target.innerHTML = input.value;
};
input.onfocus = function(e) { 
evt.target.innerHTML = input.value;
};
input.onblur = function(e) {
input.remove();
};
input.style.fontSize = "1.25em"
input.style.padding = "0"
input.style.border = "none"
input.style.height = "1px"
input.style.width = "4px"
const myCells = document.querySelectorAll('.myForeignObject');
for (let i=0; i < myCells.length; i++) {
myCells[i].appendChild(input)
}
}

问题截图:
输入框聚焦时的黑色异常


内容的提问来源于stack exchange,提问作者Eric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:40:08