如何让我的插入节点停止突出显示?
我有一个用于我正在制作的 chrome 扩展的 Javascript 函数,它显示一个弹出字典。这是通过在用户用鼠标突出显示的文本下方插入一个跨度节点来完成的。
function insertPopupDict() {
if (window.getSelection) {
var selection = window.getSelection();
var text = selection.toString();
var range = selection.getRangeAt(0);
var popupDictionaryWindow = document.createElement('span');
popupDictionaryWindow.id = 'hawaiian-popup-dictionary'
popupDictionaryWindow.style = 'margin-top: 35px; width: 360px;background-color: #555;color: #fff;text-align: center;border-radius: 6px;padding: 8px 0;position: absolute;z-index: 1;';
searchWord(text).then(defenitions => {
popupDictionaryWindow.innerHTML = text + "<hr><br>";
for (var i = 0; i < defenitions.length; i++) {
popupDictionaryWindow.innerHTML += defenitions[i] + "<br><br>"
}
range.insertNode(popupDictionaryWindow);
});
popupVisible = true;
}
}
弹出窗口有效,但弹出窗口中的文本也会突出显示。
有人知道如何阻止这种情况发生吗?我是 Javascript 新手,老实说,我并不完全理解选择范围是如何工作的。我想突出显示所选单词,但取消选择新弹出窗口中的任何内容。
I have a Javascript function for a chrome extension I am making that shows a popup dictionary. This is done by inserting a span node under the text the user has highlighted with their mouse.
function insertPopupDict() {
if (window.getSelection) {
var selection = window.getSelection();
var text = selection.toString();
var range = selection.getRangeAt(0);
var popupDictionaryWindow = document.createElement('span');
popupDictionaryWindow.id = 'hawaiian-popup-dictionary'
popupDictionaryWindow.style = 'margin-top: 35px; width: 360px;background-color: #555;color: #fff;text-align: center;border-radius: 6px;padding: 8px 0;position: absolute;z-index: 1;';
searchWord(text).then(defenitions => {
popupDictionaryWindow.innerHTML = text + "<hr><br>";
for (var i = 0; i < defenitions.length; i++) {
popupDictionaryWindow.innerHTML += defenitions[i] + "<br><br>"
}
range.insertNode(popupDictionaryWindow);
});
popupVisible = true;
}
}
The popup works, but the text in the popup span gets highlighted as well.
Does anybody know how to stop this from happening? I'm new to Javascript and honestly don't fully understand how the selection range works. I want to leave the selected word highlighted, but de-select anything in the new popup span.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了解决方案。仍然不完全理解为什么我必须这样做,但解决方案是在与旧范围相同的位置创建一个新范围。
Found the solution. Still don't entirely understand why I had to do this, but the solution was to just create a new range at the same spot as the old one.