如何让我的插入节点停止突出显示?

发布于 2025-01-13 18:30:51 字数 1295 浏览 2 评论 0原文

我有一个用于我正在制作的 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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼前雾蒙蒙 2025-01-20 18:30:51

找到了解决方案。仍然不完全理解为什么我必须这样做,但解决方案是在与旧范围相同的位置创建一个新范围。

function insertPopupDict() {
    if (window.getSelection) {
        let selection = window.getSelection();
        var text = selection.toString();
        var range = selection.getRangeAt(0);

        //this is the solution to the highlight problem
        var newRange = document.createRange();
        newRange.setStart(selection.focusNode, selection.startOffset);

        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>"
            }
            //place node at newRange instead of range
            newRange.insertNode(popupDictionaryWindow);
        });
        popupVisible = true;
    }
}

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.

function insertPopupDict() {
    if (window.getSelection) {
        let selection = window.getSelection();
        var text = selection.toString();
        var range = selection.getRangeAt(0);

        //this is the solution to the highlight problem
        var newRange = document.createRange();
        newRange.setStart(selection.focusNode, selection.startOffset);

        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>"
            }
            //place node at newRange instead of range
            newRange.insertNode(popupDictionaryWindow);
        });
        popupVisible = true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文