【发布时间】:2016-02-21 14:50:51
【问题描述】:
我需要使用 jQuery 搜索文档以找到特定的单词。它实际上是一个品牌名称,无论在何处使用,都必须是粗体和斜体。
我可以使用:contain 做到这一点,但只能基于单个元素。我需要能够浏览锚点、列表 div 等。
$( "a:contains('brand name')" ).html().replace('brand name'....
任何想法都将不胜感激。
更新: 我已经做到了这一点,它可以工作并替换页面上的所有内容,但我现在需要用一个类包装一个跨度。如此接近,但难住了这一点。再次提出想法将不胜感激。
$("body *").contents().each(function() {
if(this.nodeType==3){
this.nodeValue = this.nodeValue.replace(/brandname/g, 'colour');
}
});
【问题讨论】:
-
$( ":contains('brand name')" ) 将返回所有元素。
-
It's actually a brand name that has to be bold and italic wherever used&I can do this using :contain but only on an individual element basis. I need to be able to go through anchors, lists divs etc.但是在b元素中包装div不是有效的 HTML 标记。不太清楚你在期待什么? -
@A.Wolff - 在任何情况下都期望单词并且只有单词“BrandName”被包裹在跨度中。不是BrandName,而是BrandName
标签: jquery replace contains nodevalue