如何在jQuery中以#标签开头的单词中添加类? [英] How to add class to words starting with a hashtag in jQuery?

查看:65
本文介绍了如何在jQuery中以#标签开头的单词中添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我在jQuery中有一个看起来像这样的字符串

So let's say I have a string in jQuery that looks like

    result.text = "I tweeted something #one #two #three";

有没有一种方法可以将所有以散列标签开头的单词包装在一个名为"tweet-hash"的跨度类中?

Is there a way to wrap all the words starting with a hashtag with a span class called "tweet-hash" ?

推荐答案

您可能想使用一些Regex查找符号/单词组合,并将其替换并用样式标签和CSS类包装.不幸的是,CSS没有这样的值选择器.

You'll probably want to use some Regex to find the symbol/word combination, and replace it and wrap it with a style tag and a CSS class. CSS unfortunately doesn't have a selector for values like that.

$("#phrase").html($("#phrase").html().replace(/#([^ ]+)/, "<span class='hashtag'>$1</span>")

这篇关于如何在jQuery中以#标签开头的单词中添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆