CKEDITOR-将粗体应用于包含数字的编号列表
CKEDITOR - Apply bold to numbered list including numbers
ckeditor
中的编号列表出现问题。当我尝试在li
中加粗某些文本时,只有文本变为粗体,而没有前面的数字。这就是它的样子,
- 一
- 两个
- 三
它应该像这个
2.两个
当我检查源代码时,我发现了下面的代码
<li><strong>Two</strong></li>
我想知道有没有办法改变bold
按钮的工作方式,这样它就会在下面添加一些类似的东西
<li style="font-weight:bold">Two</li>
<p> Hello <strong>World</strong></p>
我试图解决你的问题。
我的解决方案不是最好的,因为我想创建一个大胆的插件,关心列表项将是最好的解决方案。
我没有使用jQuery;然而,使用它,代码应该变得更简单、可读性更强。
-
首先,我们需要定义一些对主要任务有用的东西:
-
字符串修剪。看看这个。
if (!String.prototype.trim) { String.prototype.trim = function() { return this.replace(/^'s+|'s+$/g, ''); }; }
-
字符串包含。查看此
String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
-
第一个子元素。以下函数获取作为参数传递的元素的第一个子元素,或者不是空文本节点
function getFirstChildNotEmpty(el) { var firstChild = el.firstChild; while(firstChild) { if(firstChild.nodeType == 3 && firstChild.nodeValue && firstChild.nodeValue.trim() != '') { return firstChild; } else if (firstChild.nodeType == 1) { return firstChild; } firstChild = firstChild.nextSibling; } return firstChild; }
-
-
现在,我们可以定义我们需要的两个主要功能:
function removeBoldIfPresent(el) { el = el.$; var elStyle = el.getAttribute("style"); elStyle = (elStyle) ? elStyle : ''; if(elStyle.trim() != '' && elStyle.contains("font-weight:bold")) { el.setAttribute("style", elStyle.replace("font-weight:bold", '')); } } CKEDITOR.instances.yourinstance.on("change", function(ev) { var liEls = ev.editor.document.find("ol li"); for(var i=0; i<liEls.count(); ++i) { var el = liEls.getItem(i); var nativeEl = el.$.cloneNode(true); nativeEl.normalize(); var firstChild = getFirstChildNotEmpty(nativeEl); if(firstChild.nodeType != 1) { removeBoldIfPresent(el); continue; } var firstChildTagName = firstChild.tagName.toLowerCase() if(firstChildTagName == 'b' || firstChildTagName == 'strong') { //TODO: you also need to consider the case in which the bold is done using the style property //My suggest is to use jQuery; you can follow this question: https://stackoverflow.com/questions/10877903/check-if-text-in-cell-is-bold var textOfFirstChild = (new CKEDITOR.dom.element(firstChild)).getText().trim(); var textOfLi = el.getText().trim(); if(textOfFirstChild == textOfLi) { //Need to make bold var elStyle = el.getAttribute("style"); elStyle = (elStyle) ? elStyle : ''; if(elStyle.trim() == '' || !elStyle.contains("font-weight:bold")) { el.setAttribute("style", elStyle + ";font-weight:bold;"); } } else { removeBoldIfPresent(el); } } else { removeBoldIfPresent(el); } } });
您需要使用最新版本的CkEditor(4.3版)和onchange插件(默认包含在完整包中)。
CKEditor 4.1删除其规则中未指定的类、样式和属性。
如果这是问题所在,你可能想通过添加以下行来禁用它:
CKEDITOR.config.allowedContent = true;
以下是使用它的完整代码:
window.onload = function() {
CKEDITOR.replace( 'txt_description' );
CKEDITOR.config.allowedContent = true; //please add this line after your CKEditor initialized
};
请在这里查看
<ul class="test">
<li><span>hello</span></li>
</ul>
.test li
{
font-weight:bold;
}
.test li span
{
font-weight:normal;
}
相关文章:
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 测试索引值是否等于某个数字的倍数
- 将数字转换为一定数量的硬币
- 键入最后一位数字后自动提交
- 如何在javascript中迭代数字列表
- Javascript逻辑运算符和字符串/数字
- 如何在javascript中获得与特定数字相等的随机数
- 如果元素's的ID以数字开头
- 递增一个数字而不去掉前导零
- 如何使用JavaScript查找1和N之间的所有数字的总和
- 如何在这里将两个值最低的数字相加
- 如何删除除冒号、数字和'上午'或者'下午'
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- CKEDITOR-将粗体应用于包含数字的编号列表
- 使用 jquery 编号格式化程序格式化数字并重新格式化它们
- 表单字段输入编号通过在前面添加零来自动转换为 3 位数字
- 获取文本信息,对它们进行排序并编号,然后放入";;在数字和每个块之后
- 编号每一个数字
- 谷歌地图编号标记都显示相同的数字
- 自定义Tinymce以将样式设置为编号有序列表中的数字