在Wordpress中使用滚动功能进行搜索?

时间:2018-12-18 05:12:07

标签: javascript wordpress

我有一个wordpress网站,一页上有300个PDF文件。我需要与wordpress页面内的浏览器中的CTRL-F函数完全一样的东西。有什么办法可以做到吗?

我尝试了一切...一步步菜鸟很棒!

1 个答案:

答案 0 :(得分:0)

基于this。很容易找到,下次再做更好的研究。

const form = document.getElementById('form');
const input = document.getElementById('input');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  findString(input.value);
});

function findString(str) {
  let strFound;
  strFound = self.find(str);
  
  if (!strFound) {
    strFound = self.find(str, 0, 1);
    while (self.find(str, 0, 1)) continue;
  }
  
  if (!strFound) alert("String '" + str + "' not found!")
  return;
}
<form id="form">
  <input id="input" type="text" placeholder="Insert text to search" />
  <br /> <br /> 
  
  What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it
  to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages,
  and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
  The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem
  Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</form>