简体   繁体   English

是否可以在HTML / CSS / JS中使用语法高亮显示 <textarea> ?

[英]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属性,可以在任何元素(如divspan等)中编辑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.

相关问题 html 中可编辑文本区域的语法高亮显示 - Syntax Highlighting for editable textarea in html 将HTML语法高亮显示到textarea中以编写代码 - Implement HTML syntax highlighting into a textarea for writing code 突出显示JS,CSS和XML / HTML的语法的通用格式或约定? - Common format or convention for syntax highlighting JS, CSS and XML/HTML? 语法高亮仅适用于 HTML 和 CSS - Syntax highlighting only with HTML and CSS 在textarea中突出显示语法? - Syntax highlighting in a textarea? 有没有办法让CSS语法突出显示 <textarea> 使用任何书签? - Is there a way to give CSS syntax highlighting to any <textarea> using any bookmarklets? 如何在 Node.js REPL 中突出显示输入文本的语法? - How to have syntax highlighting of input text in Node.js REPL? Reveal.js HTML代码语法高亮显示而不呈现它 - Reveal.js HTML Code syntax highlighting without rendering it 可以动态突出显示语法的文本区域? - Textarea that can do syntax highlighting on the fly? 具有自动完成功能和语法突出显示功能的Java语言的Textarea - Textarea for Javascript language with autocomplete and syntax highlighting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM