首页 > 解决方案 > 如何使用 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 中的文本类>

标签: jquery

解决方案


当然可能......假设条件是将>字符作为<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>


推荐阅读