几年前,我在网络论坛上添加了"智能引用".基本上,用户在先前对话中选择一个部分并单击按钮来引用它.脚本获取引用的HTML并上到DOM树以找出谁说的那样.
我只能为IE做这件事,虽然我记得很努力.但是,没有stackoverflow.com,Firefox也不那么成熟.我想现在,在Firefox中这样做很容易.这是代码的关键部分.
range2Copy = frameDoc.selection.createRange();
html2Copy = range2Copy.htmlText;
el = range2Copy.parentElement();
// go up the HTML tree until post row node (id=postrowNNNN)
while (el.nodeName != 'BODY' &&
!el.id.match(/postrow/)) {
el = el.parentNode;
}
Run Code Online (Sandbox Code Playgroud)
元素frameDoc包含用户选择文本的上一个线程.如果它没有多大意义,请在此处查看整个代码.它是FCKeditor的插件.
好吧,我试图在firefox中运行你的代码并且它不起作用,这是修改后的版本:
var selection = window.getSelection();
var node = selection.anchorNode;
while (node.nodeName != 'BODY' && !node.id.match(/postrow/)){
node = node.parentNode;
}
Run Code Online (Sandbox Code Playgroud)