Jquery-syntax dynamic Syntax Coloring sample code _jquery

Source: Internet
Author: User
Decompression Jquery-syntax-3.1.1.zip, will be public in our own directory, in the Web page to introduce the following 3 JS (download address is http://download.csdn.net/detail/liumengcheng/7344667)
Copy Code code as follows:

<script type= "Text/javascript" charset= "UTF-8" src= "Scripts/jquery-1.8.2.js" ></script>
<script type= "Text/javascript" charset= "UTF-8" src= "Public/jquery.syntax.js" ></script>
<script type= "Text/javascript" charset= "UTF-8" src= "Public/jquery.syntax.cache.js" ></script>

HTML section:
Copy Code code as follows:

<div id= "Div_configxml" style= "float:right;width:600px;" >
</div>

JS section:
Copy code code as follows:

The default type of configuration file is XML
var filetype= "xml";
When a file in the tree is selected
$ (' #tree1 '). Tree ({
Onclick:function (node) {
if (Node.children==null | | node.children==undefined) {
var url= ". /hub/config/configread? " + ' ip= ' +ip+ ' &port= ' +port+ ' &file= ' +node.id;
var str=node.id;
FileType = str.substring (Str.lastindexof (".") +1,str.length);
Filetype=filetype.tolowercase ();
$.get (Url,{},function (data,textstatus) {
Because you need multiple syntax coloring, and each selected file is different,

So each time you want to delete the original pre element and add the pre, and syntax have to be dynamically based on the suffix of the filename.

Note: Do not position the pre directly, because the pre element will disappear after each rendering, so I use a div to put the pre, every time I clean the pre, just delete all the elements under the Div.
$ ("#div_configXML *"). Remove ();
var $pre =$ ("<pre id= ' configxml ' class= ' syntax ' +filetype+" ' > </pre> ');
$ ("#div_configXML"). Append ($pre);
$ ("#configXML"). Text (data);
$.syntax ();
}
);
}
}

});

Here is the effect chart:

displaying XML files

displaying Java files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.