当前位置:首页 > JavaScript

js 实现ctrl f

2026-01-30 11:05:05JavaScript

js 实现ctrl f

js 实现ctrl f

在JavaScript中实现类似Ctrl+F的搜索功能

通过监听键盘事件和操作DOM元素,可以在网页中实现类似浏览器原生Ctrl+F的搜索功能。以下是具体实现方法:

监听Ctrl+F组合键

document.addEventListener('keydown', function(event) {
  if (event.ctrlKey && event.key === 'f') {
    event.preventDefault();
    showSearchBox();
  }
});

创建搜索框UI

function showSearchBox() {
  const searchBox = document.createElement('div');
  searchBox.innerHTML = `
    <input type="text" id="searchInput" placeholder="搜索...">
    <button id="searchNext">下一个</button>
    <button id="searchPrev">上一个</button>
    <span id="matchCount">0/0</span>
  `;
  searchBox.style.position = 'fixed';
  searchBox.style.top = '10px';
  searchBox.style.right = '10px';
  searchBox.style.zIndex = '9999';
  document.body.appendChild(searchBox);

  document.getElementById('searchInput').focus();
}

实现搜索逻辑

let currentIndex = -1;
let matches = [];

function performSearch() {
  const searchTerm = document.getElementById('searchInput').value.toLowerCase();
  if (!searchTerm) return;

  const textNodes = [];
  const walker = document.createTreeWalker(
    document.body,
    NodeFilter.SHOW_TEXT,
    null,
    false
  );

  let node;
  while (node = walker.nextNode()) {
    if (node.nodeValue.trim()) {
      textNodes.push(node);
    }
  }

  matches = [];
  textNodes.forEach((node, i) => {
    const content = node.nodeValue.toLowerCase();
    let pos = content.indexOf(searchTerm);
    while (pos !== -1) {
      matches.push({
        node,
        pos,
        length: searchTerm.length
      });
      pos = content.indexOf(searchTerm, pos + 1);
    }
  });

  updateMatchCount();
}

高亮和导航搜索结果

function highlightMatch(index) {
  if (matches.length === 0 || index < 0 || index >= matches.length) return;

  const match = matches[index];
  const range = document.createRange();
  range.setStart(match.node, match.pos);
  range.setEnd(match.node, match.pos + match.length);

  // 移除之前的高亮
  const oldHighlights = document.querySelectorAll('.search-highlight');
  oldHighlights.forEach(el => {
    const parent = el.parentNode;
    parent.replaceChild(document.createTextNode(el.textContent), el);
    parent.normalize();
  });

  // 添加新高亮
  const span = document.createElement('span');
  span.className = 'search-highlight';
  span.style.backgroundColor = 'yellow';
  range.surroundContents(span);

  // 滚动到高亮位置
  span.scrollIntoView({ behavior: 'smooth', block: 'center' });

  currentIndex = index;
  updateMatchCount();
}

function updateMatchCount() {
  const countElement = document.getElementById('matchCount');
  countElement.textContent = `${currentIndex + 1}/${matches.length}`;
}

添加事件监听器

document.getElementById('searchInput').addEventListener('input', performSearch);
document.getElementById('searchNext').addEventListener('click', () => {
  if (matches.length === 0) return;
  const nextIndex = (currentIndex + 1) % matches.length;
  highlightMatch(nextIndex);
});
document.getElementById('searchPrev').addEventListener('click', () => {
  if (matches.length === 0) return;
  const prevIndex = (currentIndex - 1 + matches.length) % matches.length;
  highlightMatch(prevIndex);
});

CSS样式

.search-highlight {
  background-color: yellow;
  color: black;
}

#searchInput {
  padding: 5px;
  margin-right: 5px;
}

#matchCount {
  margin-left: 10px;
  color: #666;
}

注意事项

  • 此实现会在整个页面内容中搜索,包括动态加载的内容
  • 搜索是区分大小写的,可以通过调整代码改为不区分大小写
  • 对于大量文本内容,可能需要优化搜索性能
  • 可以添加Esc键监听来关闭搜索框
  • 考虑添加对正则表达式的支持以增强搜索功能

这种实现方式提供了基本的搜索功能,可以根据具体需求进一步扩展和完善。

标签: jsctrl
分享给朋友:

相关文章

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaSc…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js进度条实现

js进度条实现

使用HTML和CSS创建基础结构 在HTML中创建一个容器元素用于显示进度条,通常使用<div>元素。CSS用于设置进度条的样式,包括宽度、高度、颜色和圆角等属性。 <div cl…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…

js 实现页面刷新

js 实现页面刷新

使用 location.reload() location.reload() 是原生 JavaScript 提供的页面刷新方法。调用该方法会重新加载当前页面,相当于点击浏览器的刷新按钮。 // 基本…

js 实现拖拽

js 实现拖拽

实现拖拽的基本步骤 在JavaScript中实现拖拽功能需要监听几个关键事件:mousedown、mousemove和mouseup。以下是实现的基本逻辑。 监听目标元素的mousedown事件,记…