在 JQuery 中突出显示重叠文本

发布于 2024-08-25 11:09:22 字数 306 浏览 5 评论 0原文

我有这个纯 HTML:

"Many things are in my room: a bed, a desk, and a computer."

和这些短语:

"things are"
"are in my room" 
"room: a bed"

在 JQuery 中,是否有某种方法可以循环遍历短语列表,并突出显示文本中出现的短语,并用颜色或边框等来划分重叠部分?

我知道有简单的荧光笔,但这并不能解决问题。也许是有重叠不透明度的东西?谢谢!

I've got this plain HTML:

"Many things are in my room: a bed, a desk, and a computer."

And these phrases:

"things are"
"are in my room" 
"room: a bed"

In JQuery, is there some way to loop through the phrase list, and highlight the phrases as they appear in the text, and have the overlap delineated by color, or border, etc?

I know there are simple highlighters but that won't do the trick. Maybe something with overlaying opacities? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

软甜啾 2024-09-01 11:09:22

不幸的是,我所知道的荧光笔都不能满足您的要求,特别是因为 HTML 只是纯文本。

移动高级荧光笔只会将三个独立的块压缩成一个整体并突出显示所有内容。

如果您确实需要做类似的事情,您可以获取每个块并比较它是否有重叠。然后,如果有的话,从每个块中删除重叠部分并创建一个新的“重叠”突出显示(如果有意义的话)。

Unfortunately none of the highlighters I know can do what you are asking, specially because the HTML is simply plain text.

Move advanced highlighters would simply condense the three separated chunks into a single unity and highlight everything.

If you really need to do something like that you could get each chunk and compare if it has any overlapping. Then if it has, remove the overlapping part from each chunk and create a new "overlapped" highlight, if that makes sense.

栖迟 2024-09-01 11:09:22

有一个名为 anotaterjs 的很棒的 js 东西,它可能能够做到这一点。它支持嵌套突出显示,并允许您向突出显示添加注释。

在此处输入图像描述

There is this great js thinggy called anotaterjs that's probably able to do just that. It supports nested highlightings and allows you to add a note to your highlighting.

enter image description here

转身泪倾城 2024-09-01 11:09:22

我必须实现类似的目标,并且我为此付出了很多努力。
我找到了类似这样的解决方案。

http://jsfiddle.net/pw9kkg2x/34/

String.prototype.setEvidence = function(option) {
    var _parent = option.parent; //Mandatory
    var _ele = option.element || undefined; //optional
    var _pos = option.position || undefined; //optional

    if (typeof this === 'object') {
        _searchKey = this;
        pos = {}
        if (typeof _pos == 'undefined') {

            _pos = {};
            _pos.begin = $("." + _parent).html().indexOf(_searchKey);

        }
        var _content_string = $("." + _parent).html();
        _content_string = _content_string.substring(0, _pos.begin) + '<span class="_tmp_' + _ele.name + ' _tmp_span"></span>' + _content_string.substring(_pos.begin);

        $("." + _parent).html(_content_string);

        pos = $('._tmp_' + _ele.name).offset();
        console.log(pos);

        $("." + _parent).html(function(index, html) {
            return html.replace('<span class="_tmp_' + _ele.name + ' _tmp_span"></span>', '');
        });

        $("." + _parent).parent().prepend('<code id="' + _ele.id + '"><span class="_code_string ' + _ele.name + '" style="left:' + pos.left + '">' + _searchKey + '</span></code>');

        $('#' + _ele.id).offset({
            top: pos.top,
            left: 0
        });

        $('#' + _ele.id + ' span').css('marginLeft', pos.left + 'px');

    }
}


var searchKey = "simply dummy";
var searchKey2 = "Ipsum is simply dummy";
var searchKey3 = "galley of type and scrambled";
var searchKey4 = "scrambled it to make a type";

searchKey.setEvidence({
    parent : 'container',
    element : {
        name: 'container1',
        id : 'trialId1',
        class : '',
    }
});
searchKey2.setEvidence({
    parent : 'container',
    element : {
        name: 'container2',
        id : 'trialId2',
        class : '',
    }
});

searchKey3.setEvidence({
    parent: 'container',
    element: {
        name: 'container2',
        id: 'trialId3',
        class: '',
    },
    position: {
        begin: 230,
        end: 258
    }
});

searchKey4.setEvidence({
    parent: 'container',
    element: {
        name: 'container1',
        id: 'trialId4',
        class: '',
    },
    position: {
        begin: 249,
        end: 276
    }
});

I had to achieve something similar and I worked on it a lot.
I found a solution something like this.

http://jsfiddle.net/pw9kkg2x/34/

String.prototype.setEvidence = function(option) {
    var _parent = option.parent; //Mandatory
    var _ele = option.element || undefined; //optional
    var _pos = option.position || undefined; //optional

    if (typeof this === 'object') {
        _searchKey = this;
        pos = {}
        if (typeof _pos == 'undefined') {

            _pos = {};
            _pos.begin = $("." + _parent).html().indexOf(_searchKey);

        }
        var _content_string = $("." + _parent).html();
        _content_string = _content_string.substring(0, _pos.begin) + '<span class="_tmp_' + _ele.name + ' _tmp_span"></span>' + _content_string.substring(_pos.begin);

        $("." + _parent).html(_content_string);

        pos = $('._tmp_' + _ele.name).offset();
        console.log(pos);

        $("." + _parent).html(function(index, html) {
            return html.replace('<span class="_tmp_' + _ele.name + ' _tmp_span"></span>', '');
        });

        $("." + _parent).parent().prepend('<code id="' + _ele.id + '"><span class="_code_string ' + _ele.name + '" style="left:' + pos.left + '">' + _searchKey + '</span></code>');

        $('#' + _ele.id).offset({
            top: pos.top,
            left: 0
        });

        $('#' + _ele.id + ' span').css('marginLeft', pos.left + 'px');

    }
}


var searchKey = "simply dummy";
var searchKey2 = "Ipsum is simply dummy";
var searchKey3 = "galley of type and scrambled";
var searchKey4 = "scrambled it to make a type";

searchKey.setEvidence({
    parent : 'container',
    element : {
        name: 'container1',
        id : 'trialId1',
        class : '',
    }
});
searchKey2.setEvidence({
    parent : 'container',
    element : {
        name: 'container2',
        id : 'trialId2',
        class : '',
    }
});

searchKey3.setEvidence({
    parent: 'container',
    element: {
        name: 'container2',
        id: 'trialId3',
        class: '',
    },
    position: {
        begin: 230,
        end: 258
    }
});

searchKey4.setEvidence({
    parent: 'container',
    element: {
        name: 'container1',
        id: 'trialId4',
        class: '',
    },
    position: {
        begin: 249,
        end: 276
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文