Javascript: 根据单词改变单词颜色?
Javascript: Change word colors depending on the word?
抱歉,这个问题有点混乱。
我想根据单词的含义更改句子中单词的背景颜色。
我希望用户能够像使用文本区域一样输入单词,但文本区域不支持单词背景颜色。
目前我只能想到超级盗版解决方案,例如:
var div = document.getElementById("div");
function highlight() {
var words = div.textContent.split(" ");
div.innerHTML = "";
for (var i = 0; i < words.length; i++) {
if (words[i] === "good") {
div.innerHTML += "<span style=\"background-color: green;\">" + words[i] + "</span> ";
} else if (words[i] === "bad") {
div.innerHTML += "<span style=\"background-color: red;\">" + words[i] + "</span> ";
} else {
div.innerHTML += "<span>" + words[i] + "</span> ";
}
}
var range = document.createRange();
var sel = window.getSelection();
range.setStart(div.lastChild, 1);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
};
div.addEventListener("input", highlight);
highlight();
div {
background-color: black;
box-sizing: border-box;
color: white;
height: 128px;
padding: 16px;
width: 100%;
}
<div id="div" contenteditable="true">good and bad colors</div>
单词 'good' 的背景为绿色,单词 'bad' 的背景为红色。
然而,这是非常糟糕和可怕的,所以我希望有更好的方法来做到这一点。
任何帮助都会非常棒!
尝试使用数组方法,它们比 for
循环更好用:
const div = document.getElementById("div");
function highlight() {
const words = div.textContent.split(" ");
div.innerHTML = "";
words.forEach((word) => {
const span = div.appendChild(document.createElement('span'));
span.textContent = word + ' ';
if (word === 'good') span.classList.add('green');
if (word === 'bad') span.classList.add('red');
});
};
div.addEventListener("blur", highlight);
highlight();
div {
background-color: black;
box-sizing: border-box;
color: white;
height: 128px;
padding: 16px;
width: 100%;
}
.green {
background-color: green;
}
.red {
background-color: red;
}
<div id="div" contenteditable="true">good and bad colors</div>
抱歉,这个问题有点混乱。
我想根据单词的含义更改句子中单词的背景颜色。
我希望用户能够像使用文本区域一样输入单词,但文本区域不支持单词背景颜色。
目前我只能想到超级盗版解决方案,例如:
var div = document.getElementById("div");
function highlight() {
var words = div.textContent.split(" ");
div.innerHTML = "";
for (var i = 0; i < words.length; i++) {
if (words[i] === "good") {
div.innerHTML += "<span style=\"background-color: green;\">" + words[i] + "</span> ";
} else if (words[i] === "bad") {
div.innerHTML += "<span style=\"background-color: red;\">" + words[i] + "</span> ";
} else {
div.innerHTML += "<span>" + words[i] + "</span> ";
}
}
var range = document.createRange();
var sel = window.getSelection();
range.setStart(div.lastChild, 1);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
};
div.addEventListener("input", highlight);
highlight();
div {
background-color: black;
box-sizing: border-box;
color: white;
height: 128px;
padding: 16px;
width: 100%;
}
<div id="div" contenteditable="true">good and bad colors</div>
单词 'good' 的背景为绿色,单词 'bad' 的背景为红色。
然而,这是非常糟糕和可怕的,所以我希望有更好的方法来做到这一点。
任何帮助都会非常棒!
尝试使用数组方法,它们比 for
循环更好用:
const div = document.getElementById("div");
function highlight() {
const words = div.textContent.split(" ");
div.innerHTML = "";
words.forEach((word) => {
const span = div.appendChild(document.createElement('span'));
span.textContent = word + ' ';
if (word === 'good') span.classList.add('green');
if (word === 'bad') span.classList.add('red');
});
};
div.addEventListener("blur", highlight);
highlight();
div {
background-color: black;
box-sizing: border-box;
color: white;
height: 128px;
padding: 16px;
width: 100%;
}
.green {
background-color: green;
}
.red {
background-color: red;
}
<div id="div" contenteditable="true">good and bad colors</div>