jquery.highlight.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. jQuery.fn.highlight = function (pat) {
  2. function innerHighlight(node, pat) {
  3. var skip = 0;
  4. if (node.nodeType == 3) {
  5. var pos = node.data.toUpperCase().indexOf(pat);
  6. if (pos >= 0) {
  7. var spannode = document.createElement('span');
  8. spannode.className = 'app-faq-highlight';
  9. var middlebit = node.splitText(pos);
  10. var endbit = middlebit.splitText(pat.length);
  11. var middleclone = middlebit.cloneNode(true);
  12. spannode.appendChild(middleclone);
  13. middlebit.parentNode.replaceChild(spannode, middlebit);
  14. skip = 1;
  15. }
  16. } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
  17. for (var i = 0; i < node.childNodes.length; ++i) {
  18. i += innerHighlight(node.childNodes[i], pat);
  19. }
  20. }
  21. return skip;
  22. }
  23. return this.length && pat && pat.length ? this.each(function () {
  24. innerHighlight(this, pat.toUpperCase());
  25. }) : this;
  26. };
  27. jQuery.fn.removeHighlight = function () {
  28. return this.find("span.label").each(function () {
  29. this.parentNode.firstChild.nodeName;
  30. with (this.parentNode) {
  31. replaceChild(this.firstChild, this);
  32. normalize();
  33. }
  34. }).end();
  35. };