高亮匹配文本

此博客介绍了如何使用HTML和JavaScript实现在网页中输入关键词后,动态高亮显示相关文本。通过`hightLightKeyWords`函数,用户可以搜索`
    `列表中的文本,匹配到的关键字将被高亮显示。
摘要由CSDN通过智能技术生成

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .hightLight {
            color: red;
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div class="main">
        <ul>
            <li>
                aaaaaaaaaaaaaaaaaaa
            </li>
            <li>
                aaaaaaaaasgetgrthty
            </li>
            <li>
                aaaaaajhbfwkbefjkwejw
            </li>
            <li>
                aaaasndfjknwefnwkjlefn
            </li>
            <li>
                aaaasdfnjwenfkwenflewq
            </li>
        </ul>
        <input type="text" class="keywordInput"><button class="btn">搜索</button>
    </div>

    <script type="module" src="./hightLightKeyWords.js"></script>
</body>

</html>

hightLightKeyWords

import $ from 'jquery';

function hightLightKeyWords($el, kw) {
    if (!kw) return;

    try {
        kw = decodeURIComponent(kw)
    } catch (ex) {
        console.error(ex);
        return;
    }

    kw = kw.split(' ');
    kw.forEach((str) => {
        str = str.trim();
        // 关键词中不允许存在指定的特殊字符
        if (!str || str.indexOf('^') > -1 || str.indexOf('|') > -1) return console.log('禁止高亮程序占用字符');

        str = str
            .replace(/\./g, '\\.')
            .replace(/\(/g, '\\(')
            .replace(/\)/g, '\\)')
            .replace(/-/g, '\\-')
            .replace(/\+/g, '\\+')
            .replace(/\?/g, '\\?')
            .replace(/\*/g, '\\*');

        const reg = new RegExp(str, 'gi');
        $el.each(function() {
            const $is = $(this).find('i');
            const iContainers = []
            if ($is.length) {
                $is.each(function(i, $i) {
                    iContainers.push($i.outerHTML);
                });
            }

            let text = $(this).html();
            if ($is.length) {
                iContainers.forEach(function(item) {
                    text = text.replace(item, '^|^'); // 把已经高亮的剔除,暂时用特殊符号'^|^'替换
                })
            }


            const matched = text.match(reg);
            if (matched) {
                text = text.replace(reg, '<i class="hightLight">' + matched[0] + '</i>');
                if ($is.length) {
                    iContainers.forEach(function(item) {
                        text = text.replace('^|^', item);
                    })
                }
                $(this).html(text);
            }
        });
    });
}
const btn = $('.btn');
btn.on('click', function() {
    const kw = $('.keywordInput').val();
    console.log('keyword...', kw);
    hightLightKeyWords($('ul li'), kw);

})
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值