记住用户名密码
在进行Discuz二次开发的时候,有时咱们须要将编辑器单独使用,因为Discuz发布内容采用了本身独有的bbcode转换方式,为了兼容咱们也须要作相应转换。javascript
本身转换不知道规则,只有经过查找源码找到具体的转换函数。php
js版本:html
引入static/js/bbcode.js
html2bbcode("html内容")//html转bbcode bbcode2html("bbcode内容")//bbcode转html
php版本
//html转bbcode require_once libfile('function/editor'); echo html2bbcode($html_str); //bbcode转html require_once libfile('function/discuzcode'); echo discuzcode($bbcode_str, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0);
以下是网友提供的一个完整使用编辑器的例子
<script type="text/javascript" src="{$_G['setting']['jspath']}forum_post.js?{VERHASH}"></script> <script src="static/js/bbcode.js?{VERHASH}" type="text/javascript"></script> <script type="text/JavaScript"> var fid = {$_G['fid']};//插件所在版块ID </script> <!--{subtemplate home/editor_image_menu}--> <textarea class="userData" name="content" id="uchome-ttHtmlEditor" style="height: 100%; width: 100%; display: none; border: 0px"></textarea> <iframe src='home.php?mod=editor&charset={CHARSET}&allowhtml=1&isportal=0' name='uchome-ifrHtmlEditor' id='uchome-ifrHtmlEditor' scrolling='no' style='width:85%;height:400px;border:1px solid #C5C5C5;position:relative;' border=0 frameborder=0 ></iframe> </br> <input type="hidden" name="formhash" id="formhash" value="{FORMHASH}" /> <input type="hidden" name="posttime" id="posttime" value="{$posttime}" /> <input type="hidden" name="wysiwyg" id="e_mode" value="1" /> <input type="hidden" name="special" value="127" /> <input type="hidden" name="specialextra" value="plugin_test" /> <input type="hidden" id="message" name="message" value="" /> <input id='submit_editsubmit' class='btn' type='button' value='提交' name='editsubmit' onClick='validate(this);'> </form> <script type="text/javascript" src="static/image/editor/editor_base.js?{VERHASH}"></script> <script type="text/javascript" src="static/image/editor/editor_function.js?{VERHASH}"></script> <script type="text/javascript"> var textobj = $('uchome-ttHtmlEditor'); var wysiwyg = (BROWSER.ie || BROWSER.firefox || (BROWSER.opera >= 9)) && parseInt('1') == 1 ? 1 : 0; var allowswitcheditor = parseInt('1'); var allowhtml = parseInt('0'); var allowsmilies = parseInt('1'); var allowbbcode = parseInt('1'); var allowimgcode = parseInt('1'); var simplodemode = parseInt('0'); var fontoptions = new Array("宋体", "新宋体", "黑体", "微软雅黑", "Arial", "Verdana", "Mingliu", "Helvetica", "Trebuchet MS", "Tahoma", "Impact", "Times New Roman", "仿宋,仿宋_GB2312", "楷体,楷体_GB2312"); var smcols = 8; var custombbcodes = new Array(); </script> <script type="text/JavaScript">
function validate(obj) { var mcpp = window.frames['uchome-ifrHtmlEditor']; var mcpobj = mcpp.window.frames['HtmlEditor']; edit_save(); jQuery("#message").val(html2bbcode(mcpobj.document.body.innerHTML)); //将编辑器内容转bbcode window.onbeforeunload = null; obj.form.submit(); return false; } </script>
在编辑时直接读取数据库中内容没法按html展现,且再次保存时会遇到“您的提交带有不合法参数,谢谢合做”,咱们须要预先将存到数据库的bbcode转成html加载到编辑器
window.onload = function(){ var mcpp = window.frames['uchome-ifrHtmlEditor']; var mcpobj = mcpp.window.frames['HtmlEditor']; mcpobj.document.body.innerHTML = bbcode2html("{$thread['message']}");//加载完成时将bbcode内容转换成正常的html代码并赋值}
目前有 0 条留言 其中:访客:0 条, 博主:0 条