搜索当前页面查找内容_编程笔记

搜索当前页面查找内容

2024-01-02 09:40:39  浏览:643  作者:保利尼奥
<form> <input type="search" id="searchInput" placeholder="Search..."> <button type="button" onclick="searchFunction()">Sea...
<form>
    <input type="search" id="searchInput" placeholder="Search...">
    <button type="button" onclick="searchFunction()">Search</button>
</form>

<script>
    function searchFunction() {
        var searchText = document.getElementById('searchInput').value;
        if (searchText) {
            if (window.find && window.getSelection) {
                document.designMode = "on";
                var sel = window.getSelection();
                sel.collapse(document.body, 0);
                while (window.find(searchText)) {
                    document.execCommand("HiliteColor", false, "yellow");
                    sel.collapseToEnd();
                }
                document.designMode = "off";
            } else if (document.body.createTextRange) {
                var textRange = document.body.createTextRange();
                while (textRange.findText(searchText)) {
                    textRange.execCommand("BackColor", false, "yellow");
                    textRange.collapse(false);
                }
            }
        }
    }
</script>


评论区

共 0 条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~

【随机内容】

返回顶部