插入填空题,一个看似小小的需求,但是却是折腾了很9。主要产品那边的要求,空格上面要标有序号,并且再页面当中能够同步空格答案列表。

1.ueditor插件 插件入门,官方的例子还是很简单直接的,对于我们这个功能够用了,就不做过多介绍。

2.空格序号的显示。要求显示未(  1  ),删除的时候要一起删除

  方案1: 参考的QQ空间 @功能,原来是通过img的alt属性实现的,设置个下边框,这个灵活些,数字可以随意添加,只是样式上面没办法做到很好看

方案2: 直接用图片的方式,这个就是要事先出好图,没办法支持很大,不灵活

使用的方案2,项目中一道题的填空题项目也不能很多

3.填空的唯一性

  方案1:通过实行维护index,有上一次的index和这一次的index,进行逻辑处理

  方案2:通过uuid的方式进行相关的维护,不存在uuid属性的则新设一个

使用的方案2,uuid能确保唯一性,逻辑简单,易懂

4.列表 和 填空的对应关系

  针对3的方案1,需要判断本次index和上一次的index,是新增还是删除,还是修改序列。

  针对3的方案2,列表中不存在uuid的数据,则是新增,存在的就修改下索引,超出的删除。逻辑简明易懂

看看下面的效果。也可以点这里体验

插件代码入下,

function UE_UI_BLANK (editor,uiName) {
UEDITOR_CONFIG = {
UEDITOR_IMAGE_VISIT_URL: '../'
}
var s4 = function () {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
var getUUID = function () {
return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();
}; var hasClass = function (node, name) {
return node.attrs && node.attrs["class"] && node.attrs["class"].indexOf(name) != -1;
}
var me = this;
me.addInputRule(function (root) {
$.each(root.getNodesByTagName('img'), function (i, node) {
if (hasClass(node, 'question-blank-space')) {
node.setAttr('onclick', 'return false;');
node.setAttr('ondragstart', 'return false;');
}
});
});
me.addOutputRule(function (root) {
var flag = false;
var tab = [];
$(me.body).find('img.question-blank-space').each(function (index) {
this.src = UEDITOR_CONFIG.UEDITOR_IMAGE_VISIT_URL + "third-party/blank/" + ( index + 1 ) + ".png";
this.setAttribute('alt', index + 1);
if (!this.getAttribute('uuid')) {
this.setAttribute('uuid', getUUID());
}
tab[index] = this.getAttribute('uuid');
if (index > 9) {
$(this).remove();
flag = true;
}
})
if (flag) {
Win.alert('最多插入10个填空题');
}
var id = -1;
$.each(root.getNodesByTagName('img'), function (i, node) {
if (hasClass(node, 'question-blank-space')) {
id++;
node.setAttr('uuid', tab[id]);
node.setAttr('src', UEDITOR_CONFIG.UEDITOR_IMAGE_VISIT_URL + "third-party/blank/" + ( id + 1 ) + ".png");
node.setAttr('ondragstart', '');
node.setAttr('onclick', '');
node.setAttr('onfocus', '');
}
});
});
var btn = new UE.ui.Button({
name: uiName,
title: '插入填空项',
cssRules: 'border-bottom: 1px solid black;background: none repeat scroll 0 0 #fafafa !important;',
onclick: function () {
editor.execCommand(uiName);
}
});
me.commands[uiName] = {
execCommand: function (cmd, latex) {
if ($(me.body).find('.question-blank-space').length > 9) {
alert('最多支持插入10个空');
return;
}
me.execCommand('inserthtml', '<img class="question-blank-space edui-faked-music" onfocus="return false;" ondragstart="return false;" onclick="return false;"/>');
},
queryCommandState: function (cmd) {
return 0;
},
queryCommandValue: function (cmd) {
return false;
}
}
return btn;
};

例子页面逻辑代码如下

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<link type="text/css" rel="stylesheet" href="../../../public/reset.css" >
<script type="text/javascript" charset="utf-8" src="../ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="editor_api.js"> </script>
<script type="text/javascript" charset="utf-8" src="../lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="../third-party/blank/addBlankButton.js"> </script>
<script type="text/javascript" charset="utf-8" src="../../../public/jquery-1.11.3.min.js"> </script>
<style type="text/css">
body {
padding: 10px;
}
div{
width:100%;
}
#count {
color: #5cb85c;
}
table td{
border:1px solid #000;
padding: 8px; }
.mt10 {
margin-top: 10px;
}
</style>
</head>
<body>
<div>
<div class="mt10">填空题个数:    <span id="count"></span></div>
<table class="mt10" id="blankTable">
<tr>
<td width="50px">序列</td>
<td width="200px">答案</td>
</tr>
</table>
<script class="mt10" id="editor" type="text/plain" style="width:800px;height:300px;"></script>
</div>
</body>
<script type="text/javascript">
UE.getEditor('editor',{
toolbars:[[]]
})
UE.registerUI('blank', UE_UI_BLANK);
UE.getEditor('editor').addListener( 'contentChange', function() {
var content = this.getContent();
var $tmp = $('<div>' + content + '</div>');
var $fillChar = $tmp.find(".question-blank-space");
var $table = $('#blankTable');
var len = $fillChar.size();
$("#count").html(len);
$fillChar.each(function (index) {
var $elm = $(this);
var alt = index + 1;
var uuid = $elm.attr("uuid");
var $tr = $table.find("tr.blank-line" + uuid);
if ($tr.length == 0) {//新增的
$table.find("tr:eq(" + index + ")").after('<tr class="blank-line' + uuid + '" ><td>第' + alt + '空</td><td contentEditable="true"></td></tr>');
} else if ($tr.index() != index) {
$tr.find('td:eq(0)').html('第' + alt + '空');
$table.find("tr:eq(" + index + ")").after($tr);
}
});
$table.find("tr:gt(" + len + ")").remove();
$tmp.remove();
})
</script>
</html>

ueditor插件 -- 插入填空题的更多相关文章

  1. Linux笔试常见填空题

    一.填空题: 在Linux系统中,以 文件 方式访问设备 . Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统. Linux文件系统中每个文件用 i节点 来标识. 全部磁盘块 ...

  2. YTU 2607: A代码填空题--更换火车头

    2607: A代码填空题--更换火车头 时间限制: 1 Sec  内存限制: 128 MB 提交: 91  解决: 73 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 假设火车有 ...

  3. YTU 2601: 熟悉题型——填空题(删除线性表节点)

    2601: 熟悉题型--填空题(删除线性表节点) 时间限制: 1 Sec  内存限制: 128 MB 提交: 357  解决: 212 题目描述 给出一串具体长度的数据,删除指定数据. 已经给出部分代 ...

  4. springmvc集成Ueditor插件实现图片上传2、

    一.下载Ueditor插件. 地址:http://ueditor.baidu.com/website/download.html 二.环境搭建. 具体可以参看http://fex.baidu.com/ ...

  5. kindeditor自定义插件插入视频代码

    kindeditor自定义插件插入视频代码 1.添加插件js 目录:/kindeditor/plugins/diy_video/diy_video.js KindEditor.plugin('diy_ ...

  6. moodle中的完形填空题的文本编写方法

    moodle中的完形填空题的文本编写方法 [完形填空题]考题把一段文字挖去一些空,让考生根据上下文正确地完成这些填空.完型填空题中的一段短文可以包括各种题目,如选择,填空,和数字题等. 题目的编辑是在 ...

  7. 随笔:关于 FastAdmin ueditor 插件 中的 rand mt_rand mt_getrandmax 问题

    随笔:关于 FastAdmin ueditor 插件 中的 rand mt_rand mt_getrandmax 问题 问题来源 一位小伙伴在使用 Ueditor 插件时出错,因为用的是 php7.1 ...

  8. 『Python题库 - 填空题』151道Python笔试填空题

    『Python题库 - 填空题』Python笔试填空题 part 1. Python语言概述和Python开发环境配置 part 2. Python语言基本语法元素(变量,基本数据类型, 基础运算) ...

  9. 百度编辑器ueditor插件的基本使用

    注意:该插件是基于tpframe开发,请在tpframe框架上使用 插件下载地址:https://pan.baidu.com/s/1MOJbd1goQC0Bn5-7HcIdKA 插件下载下来后解压到a ...

随机推荐

  1. node.js url模块

    URL url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) url.format(urlObj) url.resolve(from, ...

  2. VS2010中查询替换使用

    MSDN:http://msdn.microsoft.com/zh-cn/library/afy96z92.aspx 例子:

  3. iOS设计模式解析(六)代理模式

    代理模式:为其他对象创建一个代理以控制对这个对象的访问 UML解释:客户端向Proxy发起一个Request()请求,Proxy对象会把这个Request转发给Proxy对象的RealSubject. ...

  4. 解决Qt中QTableWidget类方法setItem 时导致程序崩溃问题

    在为一个音乐播放器增加功能时莫明奇妙的出现程序崩溃,定位到是由于QTableWidget 的setItem方法导致的,最终在此处找到了解决方式. 大致是说不能在setItem之前连接cellChang ...

  5. pycharm的激活

    ——————转———— Pycharm5注册方式   0x1 ,安装 0x2 , 调整时间到2038年. 0x3 ,申请30天试用 0x4, 退出pycharm 0x5, 时间调整回来. ##注册方法 ...

  6. 关于Nexus 7的Usb host开发问题

    按照API Guides和搜索到的各种方法,都没办法把Nexus 7上面的USB 设备列举出来.使用市场上的软件依然不行. 在找demo的时候找到一位大神chainfire,他似乎有所解释 看来得换一 ...

  7. ODI 11g Studio 修改界面语言

    {ODI_HOME}/oracledi\client\ide\bin找到ide.conf 添加AddVMOption -Duser.language=enAddVMOption -Duser.regi ...

  8. 在strings.xml中定义html标签

    在项目的开发过程中,需要用到把html内容放到strings.xml文件中,然后再读取到TextView中.原本以为像普通文本一样直接SetText就行了,结果行不通,大大超出我的预料.经过网上搜索, ...

  9. CSS左中右布局,规范案例

    html部分 <body> <form id="form1" runat="server"> <div id="wrap ...

  10. egret随笔-publish命令的改进

    缘由 导了几天的ipa,每次publish后都要改zip包名的代码,终于鼓起勇气翻看了一下egret publish的代码,唉,这代码...应该不会是北京的那几个大牛写的吧??? 正题 看了源码才知道 ...