将嵌套的html转换为bbCode引用标记 [英] Converting nested html to bbCode quote tags

查看:383
本文介绍了将嵌套的html转换为bbCode引用标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换以下html

I am trying to convert the following html

<div class="bbQuote">
    <div class="quoteAuthor">Joe Block</div>
    <div class="quoteContent">This is the first message<br>
        <div class="bbQuote">
            <div class="quoteAuthor">Jane Doe</div>
            <div class="quoteContent">This is the second message</div>
        </div>
    </div>
</div>

到以下bbCode

[quote=Joe Block]
    This is the first message
    [quote=Jane Doe]
        This is the second message
    [/quote]
[/quote]

如何使用jQuery执行此操作?

How can I do this using jQuery?

PS:嵌套HTML可以有零个或多个孩子

PS: Nested HTML can have zero or more children

推荐答案

这是一个非常基本的例子:

Here's a very basic example:

var html = $('#commentContent').html(),
    beingParsed = $('<div>' + html.replace(/<br>/g, '\n\r') + '</div>'),
    $quote;
while (($quote = beingParsed.find('.bbQuote:first')).length) {
    var $author = $quote.find('.quoteAuthor:first'),
        $content = $quote.find('.quoteContent:first'),
        toIndent = $author[0].previousSibling;

    toIndent.textContent = toIndent.textContent.substring(0, toIndent.textContent.length-4);
    $author.replaceWith('[quote=' + $author.text() + ']');
    $content.replaceWith($content.html());
    $quote.replaceWith($quote.html() + '[/quote]');
}

var parsedData = beingParsed.html();

小提琴

限制:


  1. 它不会将其他HTML转换为BBCode(< b> < i> ,锚标签等);

  2. 缩进/空格不是100%准确。

  1. It won't convert other HTML to BBCode (<b>, <i>, anchor tags etc);
  2. Indentation/white space is not 100% accurate.

我使用Ajax从数据库中获取实际的帖子内容或使用正确的jQuery bbCode解析库。

I'd use Ajax to fetch the actual post content from the DB or use a proper jQuery bbCode parsing library.

这篇关于将嵌套的html转换为bbCode引用标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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