最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to have syntax highlighting for HTMLCSSJS in <textarea>? - Stack Overflow

programmeradmin4浏览0评论

Here's a pen of what I mean:

 <textarea id="html" placeholder="HTML"></textarea>
    <textarea id="css" placeholder="CSS"></textarea>
    <textarea id="js" placeholder="JS"></textarea>
<iframe id="code"></iframe>

Here's a pen of what I mean:

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.

I've e across the Codemirror library but it doesn't seem like it would work for live input.

Share edited Mar 30, 2017 at 10:05 Rashmin Javiya 5,2223 gold badges30 silver badges49 bronze badges asked Mar 30, 2017 at 9:56 ArchieArchie 1071 gold badge3 silver badges7 bronze badges 1
  • you have the 2021's answer here stackoverflow./a/69984857/5781320 – user5781320 Commented Nov 16, 2021 at 6:58
Add a ment  | 

1 Answer 1

Reset to default 5

You cannot directly control syntax highlighting in a textarea. You can try JS libraries like http://www.cdolivet./editarea/ or if you just wnat to do it all by yourself, you can go for contenteditable .

Contenteditable is an html Attribute that enables textarea like editing in any element like div , span etc.

Although you will have to use a lot of javascript to interpret the language and highlight it accordingly.

<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

If planning some third party libraries you can go for highlight.js: https://highlightjs/

It can be integrated with your contenteditable.

<script src="https://cdnjs.cloudflare./ajax/libs/highlight.js/9.10.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/highlight.js/9.10.0/languages/php.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/highlight.js/9.10.0/styles/purebasic.min.css" />
<script>hljs.initHighlightingOnLoad();</script>
<pre><code class="html">class test {}</code></pre>

发布评论

评论列表(0)

  1. 暂无评论