JE 博客页面的"回复"函数quote_comment 修改版

本文介绍了一种增强版的博客回复引用功能实现方法,通过JavaScript改进了原有的引用功能,解决了多层嵌套引用的问题,但仍存在换行丢失等bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在blog 页面 中, 每个回复旁边的那个"回复"按钮 对应的是 一个类似"引用" 功能的函数
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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值