jquery - 如何使用 jquery 编写 greentext 模块
问题描述
我正在使用 JQuery 为我的网站编写一个简单的论坛,而不是使用4chan 上的 greentext[quote][/quote]
来做引用。>
所以如果你有这样的帖子
"This is a normal line
>this is a quotation"
"this is a quote" 将是绿色的,因为有>
一个新行,并且文本将是绿色的,直到有一个全新的行(按下回车键)。
我如何编写一个 jQuery 模块来实现 greentext,其中新行后面的任何内容的类>
直到color: green
下一个新行?
我特别苦苦挣扎的是如何<p>
根据检测到 a来修改 a 中的文本类>
解决方案
当然可能......假设条件是将>
字符作为<p>
.
$("p").each(function(){
// If there is a > at position 0 in a <p>
if($(this).text().indexOf(">")==0){
// Remove the > character and add a CSS class
$(this).text($(this).text().substring(1)).addClass("quote")
}
})
p.quote{
color: green;
/* feel free to add more */
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Normal string</p>
<p>>Quotation</p>
推荐阅读
- java - 有没有更好的方法来创建一个查找 ArrayList 中是否存在整数值的方法?
- git - 如何将未提交的更改放入主分支
- python - 无法为使用 PEP 517 且无法直接安装的 _ 构建轮子 - 简单解决方案
- html - 本地化 Aria 标签意味着什么?
- javascript - 无法通过 Ember JS 中的 did-update 更新跟踪的属性
- python - 未找到文件 Matplotlib 线图 2 行
- ios - 有没有办法通过 MapKit 中的单个请求获取多个 .transportTypes 的 ETA?
- java - 如何以编程方式更改视图的宽度和高度?
- database - SQL 如何处理多个进程同时将相同的记录写入同一个数据库表?
- java - Java API 的方法未被覆盖,扩展此 API 的类是否使用该方法的默认实现?