受欢迎的博客标签

一、编辑页配置

1.首先我们需要引用网页样式控制文件及脚本语言编辑文件(其中前两个是tinymce脚本文件,后两个是prism高亮文件)

 

<script type="text/javascript" src="tinymce.min.js"></script>
<script type="text/javascript" src="jquery.tinymce.min.js"></script>
<link href="prism.css" rel="stylesheet" />
<script src="prism.js"></script>

2.在body中设计好我们的文本

<textarea name="content" style="width:100%"></textarea>
3.在js中进行文本的初始化

(1)tinymce.init配置并初始化tinymce

(2)selector: "textarea"根据你的选择器来指定,我这里是textarea,可以绑定id(#···)或class(.····)

(3)plugins填写要使用的插件名称

(4)toolbar设置工具栏指定显示插件,这里我展示了三个工具栏

(5)menubar禁用菜单栏模板

(6)toolbar_items_size工具栏标签大小设置为小

(7)style_formats初始化的默认样式,这个根据自己喜好调节

(8)templates模板,toolbar3中的template选中标题后自动追加值content到文本框

(9)language:'zh_CN'语言本身默认英文,这里指定语言为中文

<script type="text/javascript">
    tinymce.init({
        selector: "textarea",
        plugins: [
            "advlist autolink autosave link image lists charmap print preview hr anchor pagebreak spellchecker",
            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
            "table contextmenu directionality emoticons template textcolor paste fullpage textcolor codesample"
        ],
 
        toolbar1: "undo redo | cut copy paste | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | styleselect formatselect fontselect fontsizeselect",
        toolbar2: " searchreplace | bullist numlist | outdent indent blockquote | link unlink anchor image media code codesample | inserttime preview | forecolor backcolor",
        toolbar3: "table | hr removeformat | subscript superscript | charmap emoticons | print fullscreen | ltr rtl | spellchecker | visualchars visualblocks nonbreaking template pagebreak restoredraft",
 
        menubar: false,
        toolbar_items_size: 'small',
 
        style_formats: [
            {title: 'Bold text', inline: 'b'},
            {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
            {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
            {title: 'Example 1', inline: 'span', classes: 'example1'},
            {title: 'Example 2', inline: 'span', classes: 'example2'},
            {title: 'Table styles'},
            {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
        ],
 
        templates: [
            {title: 'Test template 1', content: 'Test 1'},
            {title: 'Test template 2', content: 'Test 2'}
        ],
        language:'zh_CN'
    });</script>

另外当我们用codesample进行插入会产生 

class=“language-**”标签,这个一眼看去和prism高亮显示插件是迎合的。所以我们使用prism作为代码高亮插件。

(4)赋值及设置值操作

var context=tinyMCE.activeEditor.getContent();//进行值得获取
 
tinyMCE.activeEditor.setContent("你的数据");//进行值得获取

二、使用prism.js美化网页中的代码

Prism is a lightweight, extensible syntax highlighter.

Prism是一款轻量的,可扩展的语法高亮处理器。

react、MDN、SitePoint、css-tricks等网站都使用该项目

网页中会插入代码。直接把代码放入<pre></pre>标签和<code></code>标签里,也算是可以在页面中显示出来.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>使用prism美化网页中的代码</title>
	<link rel="stylesheet" href="../libaray/prismjs/prism.css">
</head>
<body>
<pre class="line-numbers"><code class="language-javascript">function add(a, b){
	return a + b;
}</code>
</pre>

<script src="../libaray/prismjs/prism.js"></script>
</body>
</html>

 

1、引用网页样式控制文件及脚本语言编辑文件(两个高亮显示脚本)

<link href="prism.css" rel="stylesheet" />
<script src="prism.js"></script>
你的编辑器保存的值取出显示到页面就可以了。
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Prism不同的使用方式</title>
		<!-- 引入prism.css文件 -->
		<link rel="stylesheet" href="./prism.css">
	</head>
	<body>
		<!-- 放置代码位置 -->
		<!-- css代码 -->
		<pre>
			<code class="language-css">
				body{
					background-color: red;
				}
			</code>
		</pre>
		
		<!-- JavaScript代码 -->
		<!-- 使用行号插件和高亮插件 -->
		<pre>
			<code class="language-javascript line-numbers">
				function Person(name, age){
					this.name = name || 'pelli';
					this.age = age || 18;
				}

				Person.prototype.sayHi = function(){
					console.log('Hi, I am ' + this.name + ', I am ' + this.age + ' years old');
				};
			</code>
		</pre>

<!-- JavaScript代码 -->
<!-- 使用行号插件和高亮插件 -->
<pre>
<code class="language-javascript line-numbers">function Person(name, age){
	this.name = name || 'pelli';
	this.age = age || 18;
}

Person.prototype.sayHi = function(){
	console.log('Hi, I am ' + this.name + ', I am ' + this.age + ' years old');
};</code>
</pre>

		<!-- 引入prism.js文件 -->
		<script src="./prism.js"></script>
	</body>
</html>

【官网】https://prismjs.com/