我正在编写一个 JavaScript 函数,用于将指向箭头从一个 html 元素移动到另一个元素。 “◀”在 HTML 中显示箭头。
问题是,虽然我可以向innerHTML 添加箭头,但我似乎无法从当前选择中删除箭头。
这是我的代码的相关部分:
var current;
function changeArrowFunction(line) {
if (typeof current != 'undefined')
current.innerHTML = current.innerHTML.replace(" ◀","");
line.innerHTML = line.innerHTML + " ◀";
current = line;
}
我尝试更改 typeof 条件或完全删除它,但没有任何改善的迹象,所以问题似乎出在 Replace() 上。
最佳答案
问题是内部 html 不保留 html 代码。
如果您记录/警报 innerHTML
的值,您可以看到字符 ◀
可见,而不是字符串 ◀
,所以替换函数将无法找到字符序列来替换它。
var current;
function changeArrayFunction(line) {
if (typeof current != 'undefined') {
current.innerHTML = current.innerHTML.replace(" ◀", "");
}
line.innerHTML = line.innerHTML + " ◀";
current = line;
}
var c = 0;
function test() {
changeArrayFunction(document.getElementById('d-' + (++c)))
}
<div id="d-1">adf</div>
<div id="d-2">adf</div>
<div id="d-3">adf</div>
<div id="d-4">adf</div>
<button onclick="test();">Test</button>
关于javascript - 使用带有 Unicode 符号的 string.replace() Javascript 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771963/