【问题标题】:Convert html tag in textarea to rich text将 textarea 中的 html 标签转换为富文本
【发布时间】:2013-07-22 10:59:17
【问题描述】:

我正在使用 PHP 来填充文本区域:

<textarea text-input" id="Desc" rows="15" wrap="soft" name="Desc"><?php echo $Desc; ?></textarea> 

但 textarea 中显示了 HTML 标签,这是我不想要的。

<b>Analog And Digital System</b><br />
<b>Analog System:</b><br />
A string tied to a doorknob would be an analog system. They have a value that changes steadily over time and can have any one of an infinite set of values in a range. If you put a measuring stick or ruler at a specific point along the string, you can measure the string's value every so many seconds at that point. When you watch it move, you will see it moves constantly. It doesn't instantly jump up and down the ruler. <br />
<br />
<b>Digital System:</b><br />
A digital system would be to flick the light switch on and off. There's no 'in between' values, unlike our string. If the switch you are using is not a dimmer switch, then the light is either on, or off. In this case, the transmitter is the light bulb, the media is the air, and the receiver is your eye. This would be a digital system.<br />

基本上我想做的是将&lt;b&gt;标签转换为粗体,将&lt;br&gt;标签转换为新行。

我想不使用任何编辑器。

【问题讨论】:

    标签: php javascript html textarea


    【解决方案1】:

    如果您只想显示 HTML 输出,请使用 div 标签而不是 textarea。因为,我们无法在 textarea 中显示渲染的 HTML。

    如果您想显示 HTML 输出并希望该输出可编辑,请在 div 标记上使用 contenteditable 属性。

    有关contenteditable 的更多信息及其支持,请访问

    https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable

    【讨论】:

      【解决方案2】:

      没有办法在 textarea 中进行 HTML 格式化,你必须使用 ant 编辑器,但是如果你想在 div 中显示它,那么设置属性 contentEditable="true" 就可以了

      更多参考 here(已经在 stackoverflow 上询问)

      【讨论】:

        【解决方案3】:

        为此,您应该使用基于 JavaScript 的 WYSIWYG HTML 编辑器,例如 tinymceCKEditor

        否则 HTML 代码将保持原样(纯文本)。

        但如果您使用编辑器,用户将能够编辑内容,并且内容将使用 javascript 转换为富文本(这正是编辑器神奇地为您做的事情)。

        所见即所得 = 所见即所得

        【讨论】:

          【解决方案4】:

          你应该使用这里的代码Converting <br /> into a new line for use in a text area

          <?  
             $text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";
             $breaks = array("<br />","<br>","<br/>");  
             $text = str_ireplace($breaks, "\r\n", $text);  
          ?>  
          <textarea><? echo $text; ?></textarea>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-25
            相关资源
            最近更新 更多