目前我在一个基于jquery的非常小的wysiwyg编辑器上工作。我不关心IE或Chrome,只关注Firefox。我的问题是找到选择是否在链接中以获取href属性的值(如果已设置)。单击,找到链接的节点,双击始终是正文。 它在designMode。
我的click和dblclick的事件处理程序。 vars current_selection,current_node,iframe和container是全局的。
selection_handler:function()
{
current_selection = iframe.getSelection();
current_node = current_selection.anchorNode;
if(current_node.nodeName == "#text")
{
current_node = current_node.parentNode;
}
$('#log').text(current_node.nodeName);
},
当我单击未格式化的文本时,日志会显示mit,例如'body'。当我使用execCommand('createLink',...)添加链接时,日志显示为'A'。这样可行。当我从头到尾标记2个链接的单词时,日志显示“A”。但是双击我总是得到'身体'。所以我无法获得href属性。
处理程序在init中定义:
init:function(options)
{
...
iframe = $('#wysiwyg-'+container.attr('id'))[0].contentWindow;
iframe.addEventListener('dblclick',methods.selection_handler,false);
iframe.addEventListener('click',methods.selection_handler,false);
...
}
有人知道出了什么问题吗?
答案 0 :(得分:0)
当你点击链接时,通常会选择完整的链接,选择会改变。
使用event-object来确定单击了哪个元素:
selection_handler:function(e)
{
current_selection = window.getSelection();
current_node = e.target;
//...more code
}