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

如何使用jQuery为特定单词的所有实例的/ parts /设置样式?

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

如何使用jQuery为特定单词的所有实例的/ parts /设置样式?

为了可靠地执行此操作,您必须遍历文档中的每个元素以查找文本节点,然后在其中搜索文本。(这是问题中提到的插件的作用。)

这是一个普通的Javascript /
DOM,它允许RegExp模式匹配。jQuery并没有真正为您提供任何帮助,因为选择器只能选择元素,而’:contains’选择器是递归的,因此对我们来说不太有用。

// Find text in descendents of an element, in reverse document order// pattern must be a regexp with global flag//function findText(element, pattern, callback) {    for (var childi= element.childNodes.length; childi-->0;) {        var child= element.childNodes[childi];        if (child.nodeType==1) { findText(child, pattern, callback);        } else if (child.nodeType==3) { var matches= []; var match; while (match= pattern.exec(child.data))     matches.push(match); for (var i= matches.length; i-->0;)     callback.call(window, child, matches[i]);        }    }}findText(document.body, /bBuyNowb/g, function(node, match) {    var span= document.createElement('span');    span.className= 'highlight';    node.splitText(match.index+6);    span.appendChild(node.splitText(match.index+3));    node.parentNode.insertBefore(span, node.nextSibling);});


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

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

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