【问题标题】:Search and Highlight the text in react.js在 react.js 中搜索并突出显示文本
【发布时间】:2022-10-07 13:54:07
【问题描述】:

我的搜索栏工作正常,但在搜索输入中写了一些内容后,文本没有突出显示。

`<tbody className=\'\'>
                        {onlineUserData
                            ?.filter((onlineUserInfo) => {
                                if (searchTerm === \"\") {
                                    return onlineUserInfo
                                }
                                else if (
                                    (onlineUserInfo.userName.toLowerCase().replace(/\\s+/g, `<mark>${\'\'}</mark>`).includes(searchTerm.toLowerCase().replace(/\\s+/g, `<mark>${\'\'}</mark>`))) ||
                                    (onlineUserInfo.userStandard.toLowerCase().replace(/\\s+/g, \'\').includes(searchTerm.toLowerCase().replace(/\\s+/g, \'\'))) ||
                                    (onlineUserInfo.userRole.toLowerCase().replace(/\\s+/g, \'\').includes(searchTerm.toLowerCase().replace(/\\s+/g, \'\'))) ||
                                    (onlineUserInfo.userEmail.toLowerCase().replace(/\\s+/g, \'\').includes(searchTerm.toLowerCase().replace(/\\s+/g, \'\'))) ||
                                    (onlineUserInfo.loginTime.toLowerCase().replace(/\\s+/g, \'\').includes(searchTerm.toLowerCase().replace(/\\s+/g, \'\')))
                                ) {
                                    return onlineUserInfo
                                }
                            })
                            ?.map((onlineUserInfo, index) => (
                                <tr className=\"\" key={index}>
                                    <td className=\'\'>{onlineUserInfo.serialNumber}.</td>
                                    <td className=\'\'>{onlineUserInfo.userName}</td>
                                    <td className=\'\'>{onlineUserInfo.userStandard === \"\" ? \"N/A\" : onlineUserInfo.userStandard}</td>
                                    <td className=\'\'>{onlineUserInfo.userRole}</td>
                                    <td className=\'\'>{onlineUserInfo.userEmail}</td>
                                    <td className=\'\'>{onlineUserInfo.loginTime}</td>
                                </tr>
                            ))}
                    </tbody>`

我在 userName 行中使用了 Mark 标记,只是为了检查它是否正常工作。但事实并非如此。

    标签: reactjs search


    【解决方案1】:

    请参考此链接。我希望这对你有帮助。

    https://stackoverflow.com/a/55248758/13680835

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-29
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2012-01-03
      • 1970-01-01
      相关资源
      最近更新 更多