在blog 页面 中, 每个回复旁边的那个"回复"按钮 对应的是 一个类似"引用" 功能的函数
quote_comment:
该函数实现的功能比较简单 效果也不是很好.
我做了一个增强的版本, (见后 )
效果也不是很完美 主要是解决了 多层嵌套引用的问题,
代码写的不是很好 而且没有解决换行弄丢的bug.(现在quote_comment 也有该问题)
唉 郁闷啊. 这个很难解决, 除非对字符串进行复杂的查找替换等工作.
(但是肯定解决, 毕竟这个引用功能无论多复杂 都没有语法着色来得复杂).
最好的方法我觉得需要从两方面下手 :
1是js方面写的再强大些
2是后台到前台输出的html代码结构再重新规划一下.
不过这本身就不是什么重要功能 无所谓了.
======================================
我写的这个代码并不好 还有很大优化和完善的余地 大家一起来吧
就当做个游戏.
其实 要想完美实现这个js端的引用 还是蛮难的哦 有挑战 :)
==============================
代码很长 但是其中一些是公共的功能函数, 可以提取出去的.
quote_comment:
function quote_comment(link) {
quote_user = $(link).previous(0).innerHTML;
quote_body = $(link).up().next().innerHTML.stripTags();
editor.bbcode_editor.textarea.insertAfterSelection('[quote="'+quote_user+'"]\n' + quote_body + '\n[/quote]\n');
}
该函数实现的功能比较简单 效果也不是很好.
我做了一个增强的版本, (见后 )
效果也不是很完美 主要是解决了 多层嵌套引用的问题,
代码写的不是很好 而且没有解决换行弄丢的bug.(现在quote_comment 也有该问题)
唉 郁闷啊. 这个很难解决, 除非对字符串进行复杂的查找替换等工作.
(但是肯定解决, 毕竟这个引用功能无论多复杂 都没有语法着色来得复杂).
最好的方法我觉得需要从两方面下手 :
1是js方面写的再强大些
2是后台到前台输出的html代码结构再重新规划一下.
不过这本身就不是什么重要功能 无所谓了.
======================================
我写的这个代码并不好 还有很大优化和完善的余地 大家一起来吧
就当做个游戏.
其实 要想完美实现这个js端的引用 还是蛮难的哦 有挑战 :)
==============================
代码很长 但是其中一些是公共的功能函数, 可以提取出去的.
function quote_comment(link) {
var BR='\r\n';
var maxDepth=20,depth=0;
function removeNode(el){
if (!el) { return; }
var orphanDiv= $('_orphan_div');
if (!orphanDiv) {
orphanDiv=document.createElement('div');
orphanDiv.id='_orphan_div';
orphanDiv.style.display='none';
}
orphanDiv.appendChild(el);
orphanDiv.innerHTML = '';
}
function getText(el) {
return el.innerHTML.stripTags();
//return el.textContent || el.innerText;
}
function createQuote(quoteBody){
var children=quoteBody.childElements();
for (var i=0,child;child=children[i];i++) {
if (child.className=='quote_title') {
var title=getText(child).strip();
if (!title||title=="引用") {
child.innerHTML= BR +'[quote]';
}else{
title= title.endsWith("写道")?title.substring(0,title.length-2):title;
child.innerHTML= BR +'[quote="'+ title.strip() +'"]';
}
}else if (child.className=='quote_div') {
var subQuoteBody = child.select('[class="quote_div"]');
var quoteBodyText=null;
if (subQuoteBody && subQuoteBody.length>0 && depth++<maxDepth ) {
quoteBodyText = createQuote(child);
}else{
quoteBodyText= getText(child);
}
child.innerHTML=quoteBodyText+'[/quote]'+BR;
}else{
// do nothing
}
}
return getText(quoteBody);
}
var quoteUser = getText( $(link).previous(0) );
var commentBody = $(link).up().next().cloneNode(true);
var quote=[
'[quote="'+quoteUser+'"]',
createQuote(commentBody),
'[/quote]'+BR
]
var editorArea=editor.bbcode_editor.textarea;
editorArea.insertAfterSelection(quote.join(BR));
removeNode(commentBody);
}