Jsfiddle: http://jsfiddle.net/qTEmc/1/
我需要将事件与添加在contentitable中的链接上的按键事件相关联。
如果您尝试在链接的jfiddle的可内容区域输入,您将看到它创建了一个链接,您可以在其中输入。如果你按回车键,就会转到换行符。我想要的是在新的链接中按回车键来触发一个函数。为了进度,我现在只是想让它返回一个警告。
有人知道可靠的方法吗?
您将无法检测链接本身中的键事件,因为它们不触发键事件。相反,您需要为contentitable元素调整现有的keypress
处理程序,以检查选择项,查看它是否位于链接中。这里有一个函数。我还更新了你的演示。
function selectionInsideLink() {
var node = null, sel;
// Get the selection container node
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
node = sel.getRangeAt(0).commonAncestorContainer;
}
} else if (document.selection) {
sel = document.selection;
if (sel.type != "Control") {
node = sel.createRange().parentElement();
}
}
// Check if the node is or is contained inside a link
while (node) {
if (node.nodeType == 1 && node.tagName.toLowerCase() == "a") {
return true;
}
node = node.parentNode;
}
return false;
}