栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用jQuery突出显示一个单词

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用jQuery突出显示一个单词

此页面上可用的源代码包含加密货币挖掘脚本,请使用下面的代码,或者从网站上的下载中删除挖掘脚本。!

jQuery.fn.highlight = function(pat) { function innerHighlight(node, pat) {  var skip = 0;  if (node.nodeType == 3) {   var pos = node.data.toUpperCase().indexOf(pat);   if (pos >= 0) {    var spannode = document.createElement('span');    spannode.className = 'highlight';    var middlebit = node.splitText(pos);    var endbit = middlebit.splitText(pat.length);    var middleclone = middlebit.cloneNode(true);    spannode.appendChild(middleclone);    middlebit.parentNode.replaceChild(spannode, middlebit);    skip = 1;   }  }  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {   for (var i = 0; i < node.childNodes.length; ++i) {    i += innerHighlight(node.childNodes[i], pat);   }  }  return skip; } return this.length && pat && pat.length ? this.each(function() {  innerHighlight(this, pat.toUpperCase()); }) : this;};jQuery.fn.removeHighlight = function() { return this.find("span.highlight").each(function() {  this.parentNode.firstChild.nodeName;  with (this.parentNode) {   replaceChild(this.firstChild, this);   normalize();  } }).end();};

还可以尝试原始脚本的“更新”版本。

jQuery.extend({    highlight: function (node, re, nodeName, className) {        if (node.nodeType === 3) { var match = node.data.match(re); if (match) {     var highlight = document.createElement(nodeName || 'span');     highlight.className = className || 'highlight';     var wordNode = node.splitText(match.index);     wordNode.splitText(match[0].length);     var wordClone = wordNode.cloneNode(true);     highlight.appendChild(wordClone);     wordNode.parentNode.replaceChild(highlight, wordNode);     return 1; //skip added node in parent }        } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children     !/(script|style)/i.test(node.tagName) && // ignore script and style nodes     !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted for (var i = 0; i < node.childNodes.length; i++) {     i += jQuery.highlight(node.childNodes[i], re, nodeName, className); }        }        return 0;    }});jQuery.fn.unhighlight = function (options) {    var settings = { className: 'highlight', element: 'span' };    jQuery.extend(settings, options);    return this.find(settings.element + "." + settings.className).each(function () {        var parent = this.parentNode;        parent.replaceChild(this.firstChild, this);        parent.normalize();    }).end();};jQuery.fn.highlight = function (words, options) {    var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };    jQuery.extend(settings, options);    if (words.constructor === String) {        words = [words];    }    words = jQuery.grep(words, function(word, i){      return word != '';    });    words = jQuery.map(words, function(word, i) {      return word.replace(/[-[]{}()*+?.,\^$|#s]/g, "\$&");    });    if (words.length == 0) { return this; };    var flag = settings.caseSensitive ? "" : "i";    var pattern = "(" + words.join("|") + ")";    if (settings.wordsOnly) {        pattern = "\b" + pattern + "\b";    }    var re = new RegExp(pattern, flag);    return this.each(function () {        jQuery.highlight(this, re, settings.element, settings.className);    });};


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号