问题描述
我想在页面上找到单词,用class包装span标签。在继续执行javascript之前,我几乎已经准备好了。
I would like to Find word on page, wrap in span tags with class. before proceeding to execute a javascript i have almost got ready.
如果我能解决如何做到这一点的第一部分,我想我可以得到余下的。
If i can work out how to just do this first part i think i can get the rest.
在页面上查找单词,使用应用于span标记的类包装span标记。
必须可以通过以下方式搜索:
Find word on page, wrap in span tags with a class applied to the span tags.Must then be searchable via:
$(document).ready(function(){
var injectionKey = /%id=inject%/ig;
var injectionStack = $('#%id%').html();
(function($) {
var theInjectionPage = $("span.myclass");
theInjectionPage.html(theInjectionPage.html().replace(injectionKey, injectionStack));
})(jQuery)
});
推荐答案
不要使用正则表达式。不要替换 innerHTML
的大块。不要过去Go。不要收200美元。
Do not use regex. Do not replace large chunks of innerHTML
. Do not pass Go. Do not collect $200.
仅遍历文本节点,找到目标文本并拆分以插入新元素。请参阅问题示例代码使用< a>
。
Walk over the text nodes only, finding the target text and splitting to insert new elements. See this question for example code using an <a>
.
这篇关于在页面上查找单词,用类包装span标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!