[英]Is it possible to have syntax highlighting for HTML/CSS/JS in <textarea>?
Here's a pen of what I mean: 这是我的意思:
http://codepen.io/archiehicklin/pen/oZQdEG http://codepen.io/archiehicklin/pen/oZQdEG
<textarea id="html" placeholder="HTML"></textarea> <textarea id="css" placeholder="CSS"></textarea> <textarea id="js" placeholder="JS"></textarea> <iframe id="code"></iframe>
Trying to build a small offline wysiwyg editor and was wondering if it's possible to have some form of real-time syntax highlighting for the code input into textarea - similar to codepen or jsfiddle. 试图构建一个小的离线wysiwyg编辑器,并想知道是否有可能为代码输入到textarea中使用某种形式的实时语法高亮 - 类似于codepen或jsfiddle。
I've come across the Codemirror library but it doesn't seem like it would work for live input. 我遇到了Codemirror库,但它似乎不适用于实时输入。
You cannot directly control syntax highlighting in a textarea. 您无法直接控制textarea中的语法突出显示。 You can try JS libraries like http://www.cdolivet.com/editarea/ or if you just wnat to do it all by yourself, you can go for contenteditable .
您可以尝试使用像http://www.cdolivet.com/editarea/这样的JS库,或者如果您只想自己动手做,那么您可以选择满足 。
Contenteditable is an html Attribute that enables textarea like editing in any element like div
, span
etc. Contenteditable是一个html属性,可以在任何元素(如
div
, span
等)中编辑textarea。
Although you will have to use a lot of javascript to interpret the language and highlight it accordingly. 虽然您将不得不使用大量的JavaScript来解释语言并相应地突出显示它。
<div contenteditable="true" style="width:100%;height:200px;border:1px solid #000"> <b>This is bold text</b><br/> <u>This is underlined text</u><br/> and so on..<br/> <font color="#f00">class</font> <font color="#0f0">Sample</font><br/> <em>{</em><br/> </div>
UPDATE UPDATE
If planning some third party libraries you can go for highlight.js: https://highlightjs.org/ 如果计划一些第三方库,你可以去highlight.js: https : //highlightjs.org/
It can be integrated with your contenteditable. 它可以与您的contenteditable集成。
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.10.0/highlight.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.10.0/languages/php.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.10.0/styles/purebasic.min.css" /> <script>hljs.initHighlightingOnLoad();</script> <pre><code class="html">class test {}</code></pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.