简体   繁体   English

将HTML语法高亮显示到textarea中以编写代码

[英]Implement HTML syntax highlighting into a textarea for writing code

I have a problem that seems simple but I fear that it is too complicated for just myself to implement. 我有一个看似简单的问题,但我担心这对我自己来说太复杂了。 I'm trying to implement a textarea into a webpage where a user can write html code, press a button, and the interpreted html page shows up in a div next to the code. 我正在尝试将textarea实现到一个网页,用户可以在其中编写HTML代码,按下按钮,解释的html页面显示在代码旁边的div中。 Very similar to w3schools "try it" sections. 非常类似于w3schools“试试”部分。

So far, I have everything implemented, and it works almost as intended, the code renders properly in the right place when you press a button. 到目前为止,我已经实现了所有功能,并且它几乎按预期工作,当您按下按钮时,代码可以正确地呈现在正确的位置。 The next step is to implement syntax highlighting in the textarea such that it will recolor the text based on what tag or attribute is typed. 下一步是在textarea中实现语法突出显示,以便根据键入的标记或属性重新着色文本。 Basically, I'd like the behavior to be exactly like what notepad++ does when you write html code. 基本上,我希望这个行为与编写HTML代码时的notepad ++完全一样。 text turns blue, attribute="" text turns red, etc. 文本变为蓝色,属性=“”文本变为红色等。

My thinking is that I need to somehow read what's in the textarea word by word, test against all possible tag and attribute names, and repopulate the textarea with colored text. 我的想法是,我需要以某种方式逐字地读取textarea中的内容,针对所有可能的标记和属性名称进行测试,并使用彩色文本重新填充textarea。 From what I've seen, I don't think this is possible with a traditional textarea, and I might have to implement a specialized applet in place of the textarea. 从我所看到的情况来看,我认为传统的textarea不可能实现这一点,而且我可能不得不实现一个专门的applet代替textarea。 Does anyone know what the least painful way of accomplishing this would be? 有谁知道实现这一目标的最不痛苦的方法是什么? I'm a computer science major just about to complete my degree, but my knowledge of web programming is very limited and I've only started delving into jquery and jsp, though I have a good amount of experience with HTML/CSS/Javascript. 我是一名即将完成学位的计算机科学专业,但我对网络编程的了解非常有限,我只是开始深入研究jquery和jsp,尽管我对HTML / CSS / Javascript有很多经验。

I had a similar requirement in the past. 我过去也有类似的要求。 I found an awesome Javascript-based code editor that you can embed in your web-application. 我找到了一个很棒的基于Javascript的代码编辑器,可以嵌入到您的Web应用程序中。

Click here to view the demo: Code Mirror HTML editor demo 单击此处查看演示: Code Mirror HTML编辑器演示

Click here to download Code Mirror: Code Mirror home/download 点击这里下载Code Mirror: Code Mirror主页/下载

User Manual: Code Mirror user-manual 用户手册: Code Mirror用户手册

The editor supports syntax-highlighting, automatic indentation, intellisence auto suggestion, etc. 编辑器支持语法高亮,自动缩进,智能自动建议等。

Using it is as simple as importing the script: 使用它就像导入脚本一样简单:

<script src="codemirror.js"></script>
<link rel="stylesheet" href="codemirror.css">

And replacing the text-area with the code-mirror editor: 并使用代码镜像编辑器替换文本区域:

var myCodeMirror = CodeMirror(function(elt) {
  myTextArea.parentNode.replaceChild(elt, myTextArea);
}, {value: myTextArea.value});

If you are using JQuery, you can do it like this: 如果您使用的是JQuery,可以这样做:

var myCodeMirror = CodeMirror(function(elt) {
    $('#my-code-input').replaceWith(elt);
}, {value: $('#my-code-input').val()});

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

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