【问题标题】:Search specific word in text and add class with jQuery [duplicate]在文本中搜索特定单词并使用 jQuery 添加类 [重复]
【发布时间】:2014-06-20 09:27:15
【问题描述】:

我有这样的文字:

<p>Hi this is a text and i wan't to add a class to some words which are here</p>

而且我不想在这些词中添加类“.red”:例如,“some”、“here”、“add”。 我怎样才能用 jQuery 做这个? 代码越简单越好。

【问题讨论】:

标签: jquery html


【解决方案1】:

您可以使用此代码:

$("p").each().function(){
    $this=$(this);
    if($this.html().indexOf("some") > -1)
        $this.addClass("red");
}

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 2015-02-18
    • 2012-08-05
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多