您將無法檢測鏈接本身內的關鍵事件,因爲它們不會觸發關鍵事件。相反,您需要修改contenteditable元素的現有keypress
處理程序,以檢查該選擇是否位於鏈接中。這是一個功能。我也updated your demo。
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;
}
我有沒有告訴過你你是我的英雄?我是新來的範圍和選擇,所以這是一個巨大的幫助。謝謝。 – daveyfaherty