2 回答
TA贡献1825条经验 获得超6个赞
更新 :
var ind = 0;
let getBlocks = document.querySelectorAll(".block");
function caretInfo(up = true) {
let sel = window.getSelection();
let offset = sel.focusOffset;
if (up) {
sel.modify("move", "backward", "character");
if (offset == sel.focusOffset) return true;
else {
sel.modify("move", "forward", "character");
return false;
}
} else {
sel.modify("move", "forward", "character");
if (offset == sel.focusOffset) return true;
else {
sel.modify("move", "backward", "character");
return false;
}
}
}
function selection(item) {
getBlocks.forEach((x, index) => {
if (x === item) {
ind = index;
}
x.classList.remove("is-selected");
});
//add the class to this new focus block
item.classList.add("is-selected");
}
getBlocks.forEach((item) => {
item.addEventListener('focus', () => {
selection(item);
});
});
document.addEventListener("keydown", function(event) {
let change = false;
if (event.keyCode == 38 || event.keyCode == 40) {
if (event.keyCode == 38 && ind > 0) {
change = caretInfo();
if (change) {
ind--;
}
} else if (event.keyCode == 40 && ind < document.querySelectorAll('.root p').length - 1) {
change = caretInfo(false);
if (change) {
ind++;
}
}
if (change) {
let cur = document.querySelectorAll('.root p')[ind];
cur.focus();
}
}
})
* {
box-sizing: border-box;
}
.root {
max-width: 600px;
margin: 1rem auto;
}
.root:focus,
.block:focus {
outline: 0;
}
.is-selected {
background-color: #f4f8ff !important;
border: 1px solid #deebff !important;
}
<div class="root">
<p name="jYdi0" class="block" tabindex="0" contenteditable>1</p>
<p name="Hdy89" class="block" tabindex="0" contenteditable>It was single line but now it's multiple line Just amazine what can be happen here. Here is the problem try to click "me" and then press arrow up. Ops :( but it should be on the first line</p>
<p name="wEldk" class="block" tabindex="0" contenteditable>lorem ipsumlorem ipsumlorem ipsum3</p>
</div>
TA贡献1801条经验 获得超15个赞
你也可以做类似的事情
document.addEventListener("keypress", (event) => {
if(event.which === someEventCode) {
// your code here
}
})
在这里,someEventCode变量对于不同的按键是不同的。
添加回答
举报