简体   繁体   English

替换 [b][/b] 以在 DIV 上应用粗体文本

[英]Replacing [b][/b] to apply BOLD text on a DIV

On my Ionic App I have a Textarea where the user can select a text and with button [BOLD] add [b] [/b] around the selection.在我的 Ionic 应用程序上,我有一个 Textarea,用户可以在其中选择一个文本,并使用 [BOLD] 按钮在所选内容周围添加 [b] [/b]。

How can I apply BOLD formating, replacing the [b] [/b] on the output text?如何应用 BOLD 格式,替换输出文本上的 [b] [/b]?

Thats my code.那是我的代码。 And here's the JsFiddle这是JsFiddle

<textarea id="TheTextInput" rows="5"></textarea>
<input type="button" id="TheButton" value="click" />
<input type="button" id="bold" value="BOLD" />
<pre id="TheOutput"></pre>

$(document).ready(function () {
    $('#TheButton').click(PutTextIntoDiv);
});

function PutTextIntoDiv() {
    var TheText = encodeURIComponent($('#TheTextInput').val());
    $('#TheOutput').text(decodeURIComponent(TheText));
}

$( "#bold" ).click(function() {
    var textArea = document.getElementById("TheTextInput");
    if (typeof(textArea.selectionStart) != "undefined") {
        var begin = textArea.value.substr(0, textArea.selectionStart);
        var selection = textArea.value.substr(textArea.selectionStart, textArea.selectionEnd - textArea.selectionStart);
        var end = textArea.value.substr(textArea.selectionEnd);
        textArea.value = begin + '[b]' + selection + '[/b]' + end;
    }
});

Thanks for your help!!谢谢你的帮助!!

*Its not duplicate from Is it possible to display bold and non-bold text in a textarea? *它不是重复的是否可以在文本区域中显示粗体和非粗体文本?

Cause I don't want format the text inside the textarea, but the OUTPUT text.因为我不想格式化 textarea 内的文本,而是 OUTPUT 文本。

Consider PutTextIntoDiv() replacing with:考虑将PutTextIntoDiv()替换为:

function PutTextIntoDiv() {
    $('#TheOutput').html($('#TheTextInput').val().replace(/\[b\]/g, '<b>').replace(/\[\/b\]/g, '</b>'));
}

The <pre> in your JSFiddle should render <b> fine. JSFiddle 中的<pre>应该可以很好地呈现<b>

You can do a RegEx replace to convert the "BBCode" tags into html tags using .replace(/\\[b\\](.*)\\[\\/b\\]/g, '<b>$1</b>') .您可以使用.replace(/\\[b\\](.*)\\[\\/b\\]/g, '<b>$1</b>')进行 RegEx 替换以将“BBCode”标签转换为 html 标签.replace(/\\[b\\](.*)\\[\\/b\\]/g, '<b>$1</b>') Additionally, jQuery's .text() automatically encodes HTML entities, so you will not be able to add stylized text, so you should be using .html() instead.此外,jQuery 的.text()自动编码 HTML 实体,因此您将无法添加样式化文本,因此您应该使用.html()代替。 Overall, your code should be:总的来说,你的代码应该是:

function PutTextIntoDiv() {
    var TheText = encodeURIComponent($('#TheTextInput').val());
    $('#TheOutput').html(decodeURIComponent(TheText).replace(/\[b\](.*?)\[\/b\]/g, '<b>$1</b>'));
}

I'm not sure what the purpose of encoding and immediately decoding uri components is, but I tried to keep as much as I can untouched.我不确定编码和立即解码 uri 组件的目的是什么,但我尽量保持原状。 Here's an updated fiddle: http://jsfiddle.net/TheQueue841/62karfm1/这是一个更新的小提琴: http : //jsfiddle.net/TheQueue841/62karfm1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM